Spring 数据 mongodb - 'cursor' 选项是必需的
Spring data mongodb - The 'cursor' option is required
我正在尝试使用 Spring 数据 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(…)
使用映射的聚合管道。
我在使用 Mongodb 版本 3.6.2 时也遇到过此类错误。
检查 org.springframework.data 在 pom.xml
中的版本
对于我来说,我已经将 org.springframework.data 版本更改为 2.0。3.RELEASE 我的问题已经解决了。
我正在使用:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath></relativePath>
</parent>
然后将我的依赖升级到更高版本后,问题解决了:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath></relativePath>
</parent>
@mp911de 提到的 Pull Request 似乎已经在 Spring Data MongoDB 的 1.10.10 版本中发布。
所以你可以
- 将您的 Spring 数据 MongoDB 依赖项升级到 1.10。10.RELEASE
- 将您的 spring-boot-starter-data-mongodb 依赖升级到 1.5。10.RELEASE
Just updating the spring boot version works for me.
This is the version that I have used and worked....
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
我在使用 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)))
在这种情况下可能会有所帮助
通过将 spring 引导升级到“2.1.3.RELEASE”版本解决了这个问题。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<!--<version>1.5.10.RELEASE</version>-->
<relativePath></relativePath>
</parent>
如果您遇到此问题并且您使用的是 Studio 3T,则“选项”选项卡中有一个选项,只需选中它即可:
由于 mongodb 版本 3.6
中的驱动程序更改,您的命令不再有效。
您需要从 dbCollection.aggregate(...)
方法调用它。
升级 spring 引导版本对我不起作用。将 outputmode as cursor 更改为提供游标是强制性的,有效。在 mongo 3.6
中验证
List<DBObject> list = new ArrayList<DBObject>();
list.add(unwind.toDBObject(Aggregation.DEFAULT_CONTEXT));
list.add(group.toDBObject(Aggregation.DEFAULT_CONTEXT));
list.add(sort.toDBObject(Aggregation.DEFAULT_CONTEXT));
DBCollection col = mongoTemplate.getCollection("users");
Cursor cursor = col.aggregate(list, AggregationOptions.builder().allowDiskUse(true).outputMode(OutputMode.CURSOR).build());
List<AggregationResultVO> result = new ArrayList<AggregationResultVO>();
while(cursor.hasNext()) {
DBObject object = cursor.next();
result.add(new AggregationResultVO(object.get("aggregationResultId").toString()));
}
我已经尝试了以上所有方法(除了将代码更改为使用游标)都无济于事。
最后只好升级
'mongo-java-driver' : '3.6.2'
然后成功了。
- 使用 Spring Boot 2.0.0 或更高版本。
- 用光标将
AggregationOptions
添加到您的 Aggregation
:
AggregationOptions aggregationOptions = AggregationOptions.builder()
.allowDiskUse(true)
.cursor(new Document())
.build();
Aggregation aggregation = Aggregation.newAggregation(aggregationOperations)
.withOptions(aggregationOptions);
- 调用
MongoOperations
的 aggregateStream()
(不是 aggregate()
)方法。此方法利用由 MongoDB: 响应的光标
mongoOps.aggregateStream(aggregation, "collection", MyEntity.class)
我正在尝试使用 Spring 数据 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(…)
使用映射的聚合管道。
我在使用 Mongodb 版本 3.6.2 时也遇到过此类错误。
检查 org.springframework.data 在 pom.xml
中的版本对于我来说,我已经将 org.springframework.data 版本更改为 2.0。3.RELEASE 我的问题已经解决了。
我正在使用:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath></relativePath>
</parent>
然后将我的依赖升级到更高版本后,问题解决了:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath></relativePath>
</parent>
@mp911de 提到的 Pull Request 似乎已经在 Spring Data MongoDB 的 1.10.10 版本中发布。 所以你可以
- 将您的 Spring 数据 MongoDB 依赖项升级到 1.10。10.RELEASE
- 将您的 spring-boot-starter-data-mongodb 依赖升级到 1.5。10.RELEASE
Just updating the spring boot version works for me.
This is the version that I have used and worked....
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
我在使用 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)))
在这种情况下可能会有所帮助
通过将 spring 引导升级到“2.1.3.RELEASE”版本解决了这个问题。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<!--<version>1.5.10.RELEASE</version>-->
<relativePath></relativePath>
</parent>
如果您遇到此问题并且您使用的是 Studio 3T,则“选项”选项卡中有一个选项,只需选中它即可:
由于 mongodb 版本 3.6
中的驱动程序更改,您的命令不再有效。
您需要从 dbCollection.aggregate(...)
方法调用它。
升级 spring 引导版本对我不起作用。将 outputmode as cursor 更改为提供游标是强制性的,有效。在 mongo 3.6
中验证List<DBObject> list = new ArrayList<DBObject>();
list.add(unwind.toDBObject(Aggregation.DEFAULT_CONTEXT));
list.add(group.toDBObject(Aggregation.DEFAULT_CONTEXT));
list.add(sort.toDBObject(Aggregation.DEFAULT_CONTEXT));
DBCollection col = mongoTemplate.getCollection("users");
Cursor cursor = col.aggregate(list, AggregationOptions.builder().allowDiskUse(true).outputMode(OutputMode.CURSOR).build());
List<AggregationResultVO> result = new ArrayList<AggregationResultVO>();
while(cursor.hasNext()) {
DBObject object = cursor.next();
result.add(new AggregationResultVO(object.get("aggregationResultId").toString()));
}
我已经尝试了以上所有方法(除了将代码更改为使用游标)都无济于事。
最后只好升级
'mongo-java-driver' : '3.6.2'
然后成功了。
- 使用 Spring Boot 2.0.0 或更高版本。
- 用光标将
AggregationOptions
添加到您的Aggregation
:
AggregationOptions aggregationOptions = AggregationOptions.builder()
.allowDiskUse(true)
.cursor(new Document())
.build();
Aggregation aggregation = Aggregation.newAggregation(aggregationOperations)
.withOptions(aggregationOptions);
- 调用
MongoOperations
的aggregateStream()
(不是aggregate()
)方法。此方法利用由 MongoDB: 响应的光标
mongoOps.aggregateStream(aggregation, "collection", MyEntity.class)