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(); doubleavg = 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>再把單元測試跑起來,發現問題解決了,哈哈哈.