The 'cursor' option is required, except for aggregate with the explain argument 错误处理

这些天在用mongodb,get到了一个看起来很好用的技能.使用mongoTemplate.aggregate来进行查询,就不用麻烦的纠结mongodb的语句怎么写的了.用法如下:

 

//匹配对应postId,星级在1-5之间的数据,求平均值
TypedAggregation<Reply> aggregation = Aggregation.newAggregation(
        Reply.class,
        Aggregation.match(Criteria.where("postId").is(reply.getPostId())
                .and("starLevel").gte(IntConst.minStartLevel).lte(IntConst.maxStarLevel)),
        Aggregation.group("postId").avg("starLevel").as("avg")
);

AggregationResults<BasicDBObject> outputType = mongoTemplate.aggregate(aggregation, BasicDBObject.class);
List<BasicDBObject> replyList = outputType.getMappedResults();
double avg = 0.0;
if(replyList.size() > 0){
    avg = MathUtil.formatDecimal((double)replyList.get(0).get("avg"),IntConst.satisfactionAvgDigits);
}

但是,当我跑起来的时候,却发现总是报错,报错信息如下:

org.springframework.dao.InvalidDataAccessApiUsageException: Command execution failed:  Error [The 'cursor' option is required, except for aggregate with the explain argument], Command = { "aggregate" : "reply" , "pipeline" : [ { "$match" : { "postId" : "12313" , "starLevel" : { "$gte" : 1 , "$lte" : 5}}} , { "$group" : { "_id" : "$postId" , "avg" : { "$avg" : "$starLevel"}}}]}; 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 127.0.0.1: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 org.springframework.data.mongodb.core.MongoTemplate.handleCommandError(MongoTemplate.java:2106)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1577)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1499)
at org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1488)
at wht.forum.service.ReplyService.getSatisfaction(ReplyService.java:267)
at wht.forum.service.ReplyServiceTest.getSatisfaction(ReplyServiceTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.mongodb.MongoCommandException: Command failed with error 9: 'The 'cursor' option is required, except for aggregate with the explain argument' on server 127.0.0.1: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)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:94)
at org.springframework.data.mongodb.core.MongoTemplate.handleCommandError(MongoTemplate.java:2100)

... 33 more

 

把错误关键信息:The 'cursor' option is required, except for aggregate with the explain argument拉到百度里一搜,查到的信息很少,置顶的答案说明了问题所在,可能是mongodb的版本不兼容.但处理方案写的不是特别明白.

所以就在必应:https://cn.bing.com/里查查国外有没有人遇到过类似问题,查到确实有人在讨论: https://stackoverflow.com/questions/47472688/spring-data-mongodb-the-cursor-option-is-required#  

然后看了有一个最简单的答案,并且他说按照他的思路已经修复了问题:

恩.我也尝试一下,我本来使用的pom配置如下:

 

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

然后改成:

 

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

再把单元测试跑起来,发现问题解决了,哈哈哈.

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值