我正在try 使用Spring Data MongoDB 3.6-rc4执行聚合操作.

Aggregation agg = newAggregation(
    lookup("orders", "orderId", "_id", "order") 
);
List<BasicDBObject> results = mongoOperations.aggregate(agg, "transactions", BasicDBObject.class).getMappedResults();

但是在运行查询时会出现以下错误

2017-11-24 17:03:41,539 WARN  org.springframework.data.mongodb.core.MongoTemplate : Command execution of { "aggregate" : "transactions" , "pipeline" : [ { "$lookup" : { "from" : "orders" , "localField" : "orderId" , "foreignField" : "_id" , "as" : "order"}}]} failed: The 'cursor' option is required, except for aggregate with the explain argument
2017-11-24 17:03:41,574 ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Command execution failed:  Error [The 'cursor' option is required, except for aggregate with the explain argument], Command = { "aggregate" : "transactions" , "pipeline" : [ { "$lookup" : { "from" : "orders" , "localField" : "orderId" , "foreignField" : "_id" , "as" : "order"}}]}; nested exception is com.mongodb.MongoCommandException: Command failed with error 9: 'The 'cursor' option is required, except for aggregate with the explain argument' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "The 'cursor' option is required, except for aggregate with the explain argument", "code" : 9, "codeName" : "FailedToParse" }] with root cause
com.mongodb.MongoCommandException: Command failed with error 9: 'The 'cursor' option is required, except for aggregate with the explain argument' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "The 'cursor' option is required, except for aggregate with the explain argument", "code" : 9, "codeName" : "FailedToParse" }
    at com.mongodb.CommandResult.getException(CommandResult.java:80) ~[mongo-java-driver-3.5.0.jar:na]
    at com.mongodb.CommandResult.throwOnError(CommandResult.java:94) ~[mongo-java-driver-3.5.0.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.handleCommandError(MongoTemplate.java:2100) ~[spring-data-mongodb-1.10.8.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1577) ~[spring-data-mongodb-1.10.8.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1505) ~[spring-data-mongodb-1.10.8.RELEASE.jar:na]

提前谢谢!!

推荐答案

MongoDB在3.6中改变了聚合命令的工作方式.聚合现在需要一个光标.我们有adapted Spring Data MongoDB 2.1个,但不是以前的版本.

聚合必须通过集合的aggregate(…)方法调用,而不是直接调用命令.这也是为什么我们没有支持改变的原因.executeCommand(…)不再被调用,我们不想在错误修复版本中 destruct 兼容性.

对于您来说,最简单的方法是重写aggregate(…)方法,并使用映射的聚合管道调用适当的方法DBCollection.aggregate(…).

Mongodb相关问答推荐

如何从MongoDB集合中获取第一个和最后一个元素?

如何在MongoDB中查找和过滤嵌套数组

Mongo DB-如果一个特定字段有多个文档匹配,则更新文档字段

Next js、MongoDB和Prisma.包括不为相关字段工作

如何使用内部数组中的值更新文档

MongoDB 根据最新日期查询多个不同的值

使用 $addFields 将字段添加到 $lookup 结果中的每个项目

如何在mongoDB中按嵌套文档分组( group by )

System.FormatException occurred in MongoDB.Bson.dll - XXX is not a valid 24 digit hex string

如何在 2 个应用程序之间共享mongoose模型?

实现 twitter 和 facebook 之类的主题标签

mongo _id 字段重复键错误

获取收集字节使用情况统计信息的pymongo方法?

升级mongodb

Clojure 和 NoSQL 数据库

如何根据其他字段添加条件模式?

MEAN 堆栈文件上传

在mongoose中查询虚拟属性

将日期从毫秒转换为 ISODate 对象

MissingSchemaError:Schema hasn't been registered for model