java - Spring data mongodb - 需要 'cursor' 选项

标签 java spring mongodb spring-data-mongodb

我正在尝试使用 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(…) 不再被调用,我们不想在错误修复版本中破坏兼容性。

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

关于java - Spring data mongodb - 需要 'cursor' 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472688/

相关文章:

java - WebSphere MQ 低延迟消息传递 - 它是否具有 JMS(或类似 JMS)API?

Java,带有索引空格的格式字符串

java - Hibernate (MySQL) 上区分大小写的查询

java - 使用 Spring,如何将项目添加到最终的预填充列表中?

java - JPA 和 Hibernate 在使用 JSON 的 Spring Rest 中第一次尝试时不会更新一对多关系

mongodb - Azure 表存储的本地版本

java - 使用枚举类型重载

java - 注释 @MockBean 在 Spring Boot 2.3 上不起作用(未注入(inject)?)

mongodb - Java MongoDB 一次保存多个文档

javascript - 如何迭代对象数组以便将数据推送到 Mongodb 嵌套数组中