3

I am excecuting following mongodb query I am new to mongo db ,please tell me what i am doing wrong

   db.entityCounter.aggregate([
   {
     $lookup:
       {
         from: "fields",
         localField: "code",
         foreignField: "fieldCode",
         as: "fieldsresult"
       }
  },{
      $match:{
          $and: [{
              "fieldsresult.isVisible":"1"
              },{"type":"field"
                  }]
          }
          }])

below is java spring code

  LookupOperation lookupOperation = LookupOperation.newLookup()
            .from("fields")
            .localField("code")
            .foreignField("fieldCode")
            .as("fieldsresult");

    AggregationOperation match1 = Aggregation.match(Criteria.where("fieldsresult.isVisible").is("1"));

   // AggregationOptions aggregationOptions = Aggregation.newAggregationOptions();
    DBObject ob=new BasicDBObject();
    ((BasicDBObject) ob).put("batchSize",10);
    Aggregation aggregation = Aggregation.newAggregation(lookupOperation,match1).withOptions(Aggregation.newAggregationOptions().cursor(ob).build());



long val=0;
try {
    AggregationResults<EntityCounter> result = mongoOperations.aggregate(aggregation, Fields.class, EntityCounter.class);
    // val= result.getMappedResults();
}catch (Exception e){
    e.printStackTrace();

}

but I am getting below error

org.springframework.dao.InvalidDataAccessApiUsageException: Command execution failed: Error [The 'cursor' option is required, except for aggregate with the explain argument], Command = { "aggregate" : "entityCounter" , "pipeline" : [ { "$match" : { "fieldsresult.isVisible" : "1"}} , { "$lookup" : { "from" : "fields" , "localField" : "code" , "foreignField" : "fieldCode" , "as" : "fieldsresult"}}]}; 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" }

11
  • whats the mongodb version you are using Commented Dec 8, 2018 at 5:36
  • and also the aggregate has several pipeline and you need to use match inside the aggragete not outside Commented Dec 8, 2018 at 5:46
  • Try using cursor option available with aggregation query pipeline : cursor: { batchSize: <int> }.. useful links : a) stackoverflow.com/questions/47472688/… b) docs.mongodb.com/manual/reference/method/… Commented Dec 10, 2018 at 7:47
  • 1
    Are you sure it is right version ? Lookup support was added in spring mongo from 1.9 version. Also I cannot reproduce the error with 1.9 or any other versions. Can you create a [Minimal, Complete, and Verifiable example](stackoverflow.com/help/mcve) ? A github project will be helpful. Commented Dec 11, 2018 at 15:06
  • 1
    Its not possible. Lookup was only available from 1.9. So you can't use it when you are using 1.8. As advised please provide the complete example that can reproduce the error. Commented Dec 12, 2018 at 13:28

1 Answer 1

-1

The lookup was introduced in mongodb 3.4 please upgrade your dB

Sign up to request clarification or add additional context in comments.

6 Comments

now i upgraded my version to v4.0.2 then also i am getting same error
can you please share your mongodb debug log
vipul i have modified query but still getting error
its an issue in Spring framework if you are using spring-boot then please upgrade to 2.0 github.com/WoLpH/mongo-hacker/commit/… or use this AggregationOptions aggregationOptions=new AggregationOptions(false,false,200); and add this option at last i.e Aggregation.newAggregation(match1, lookupOperation).withOptions(aggregationOptions);
i am not using spring boot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.