Java Spring data mongodb - 'cursor' 选项是必需的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47472688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:37:12  来源:igfitidea点击:

Spring data mongodb - The 'cursor' option is required

javaspringmongodbspring-data-mongodb

提问by rohit

I am trying to execute an aggregate operation using Spring Data MongoDB 3.6-rc4.

我正在尝试使用 Spring Data MongoDB 3.6-rc4 执行聚合操作。

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

But get the following error on running the query

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

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]

Thanks in advance!!

提前致谢!!

采纳答案by mp911de

MongoDB changed in 3.6 how the aggregation command works. Aggregations require now a cursor. We adapted Spring Data MongoDB 2.1but not previous versions.

MongoDB 在 3.6 中更改了聚合命令的工作方式。聚合现在需要一个游标。我们改编了 Spring Data MongoDB 2.1但不是以前的版本。

Aggregations must be invoked through the collection's aggregate(…)method instead of calling the command directly. This is also the reason why we didn't backport the change. executeCommand(…)is no longer called and we don't want to break compatibility in a bugfix release.

必须通过集合的aggregate(…)方法调用聚合,而不是直接调用命令。这也是我们没有向后移植更改的原因。executeCommand(…)不再被调用,我们不想在错误修复版本中破坏兼容性。

The easiest approach for you can be to override the aggregate(…)method and call the appropriate method, DBCollection.aggregate(…)with the mapped aggregation pipeline.

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

回答by Adarsh Patel

I have also faced this type of error when using Mongodb version 3.6.2.

我在使用 Mongodb 3.6.2 版时也遇到过这种类型的错误。

Check your version of org.springframework.datain pom.xml

在 pom.xml 中检查您的org.springframework.data版本

For me, i have changed org.springframework.dataversion to 2.0.3.RELEASE and my problem was solved.

对我来说,我已将org.springframework.data版本更改为 2.0.3.RELEASE 并且我的问题解决了。

回答by Andrei Maimas

I was using:

我正在使用:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath></relativePath>
</parent>

Then after upgraded my dependency to a higher version, the issue was resolved:

然后将我的依赖项升级到更高版本后,问题解决了:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath></relativePath>
</parent>

回答by Atari

It seems Pull Request mentionned by @mp911de has been release in version 1.10.10 of Spring Data MongoDB. So you can either

@mp911de 提到的 Pull Request 似乎已在 Spring Data MongoDB 的 1.10.10 版中发布。所以你可以

  • upgrade your Spring Data MongoDB dependency to 1.10.10.RELEASE
  • upgrade your spring-boot-starter-data-mongodb dependency to 1.5.10.RELEASE
  • 将您的 Spring Data MongoDB 依赖项升级到 1.10.10.RELEASE
  • 升级你的 spring-boot-starter-data-mongodb 依赖到 1.5.10.RELEASE

回答by amit kumar

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>

回答by Giahu

I have also faced this type of error when using org.springframework.data version 1.10.3.RELEASE. And then i have changed version to 2.0.5.RELEASE and my problem was solved.

我在使用 org.springframework.data 版本 1.10.3.RELEASE 时也遇到过这种类型的错误。然后我将版本更改为 2.0.5.RELEASE,我的问题就解决了。

回答by mhvr

You can use cursor option available with aggregate query pipeline.

您可以使用可用于聚合查询管道的游标选项。

{cursor: { batchSize: batch_size }}

https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/

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)))may help in this case

Aggregation.newAggregation(AggregationOperation... operations).withOptions(new AggregationOptions(false,false,new Document().append("batchSize" , batch_size)))在这种情况下可能会有所帮助

回答by Andrei Maimas

Solved the issue by upgrading spring boot to the '2.1.3.RELEASE' version.

通过将 spring boot 升级到“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>

回答by Lucas Vall

If you are having this issue and you are using Studio 3T, there is an option in the Options tab, just check it:

如果您遇到此问题并且您使用的是 Studio 3T,则“选项”选项卡中有一个选项,只需检查它:

enter image description here

在此处输入图片说明

回答by krishan

Your command no longer works due to a change in mongodb driver change in version 3.6. You need to invoke it from dbCollection.aggregate(...)method instead.

由于 mongodb 驱动程序更改 version 中的更改,您的命令不再有效3.6。您需要从dbCollection.aggregate(...)方法中调用它。