Spring数据mongodb – ‘cursor’选项是必需的
我正在尝试使用Spring Data MongoDB 3.6-rc4执行聚合操作。
Aggregation agg = newAggregation( lookup("orders", "orderId", "_id", "order") ); List 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中更改了聚合命令的工作原理。 聚合现在需要一个游标。 我们改编了Spring Data MongoDB 2.1,但不是以前的版本。
必须通过集合的aggregate(…)
方法调用aggregate(…)
而不是直接调用命令。 这也是我们没有向后移动这一变化的原因。 不再调用executeCommand(…)
,我们不想破坏bugfix版本中的兼容性。
最简单的方法是覆盖aggregate(…)
方法并使用映射的聚合管道调用适当的方法DBCollection.aggregate(…)
。
我用的是:
org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE
然后在将我的依赖项升级到更高版本后,问题得以解决:
org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE
似乎@ mp911de提到的Pull Request已经在Spring Data MongoDB的1.10.10版本中发布了。 所以你也可以
- 将Spring Data MongoDB依赖项升级到1.10.10.RELEASE
- 将spring-boot-starter-data-mongodb依赖项升级到1.5.10.RELEASE
使用Mongodb版本3.6.2时,我也遇到过这种类型的错误。
检查pom.xml中的org.springframework.data版本
对我来说,我已经将org.springframework.data版本更改为2.0.3.RELEASE并且我的问题已经解决了。
Just updating the spring boot version works for me. This is the version that I have used and worked.... org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE
使用org.springframework.data版本1.10.3.RELEASE时,我也遇到过这种类型的错误。 然后我将版本更改为2.0.5.RELEASE,我的问题解决了。
您可以使用聚合查询管道提供的游标选项。
{cursor: { batchSize: batch_size }}
https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/
Aggregation.newAggregation(AggregationOperation... operations).withOptions(new AggregationOptions(false,false,new Document().append("batchSize" , batch_size)))
在这种情况下可能会有所帮助