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" }

Tejal
  • 764
  • 1
  • 10
  • 39
  • whats the mongodb version you are using – Vipul Pandey Dec 08 '18 at 05:36
  • and also the aggregate has several pipeline and you need to use match inside the aggragete not outside – Vipul Pandey Dec 08 '18 at 05:46
  • Try using cursor option available with aggregation query pipeline : cursor: { batchSize: }.. useful links : a) https://stackoverflow.com/questions/47472688/spring-data-mongodb-the-cursor-option-is-required b) https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/ – Amrit Pal Singh Dec 10 '18 at 07:47
  • What is your spring mongo jar version ? Do you have more code than what is posted ? – s7vr Dec 10 '18 at 13:26
  • I have posted aggregation related code and issue is in above code only – Tejal Dec 10 '18 at 14:56
  • I am using 3.4 version of mongodb – Tejal Dec 10 '18 at 15:14
  • I need the spring mongo jar version not mongodb. You can find that inside your pom if you have one. – s7vr Dec 10 '18 at 15:16
  • spring-data-mongodb version is 1.8.2.RELEASE – Tejal Dec 11 '18 at 05:32
  • 1
    Are you sure it is right version ? [Lookup support](https://jira.spring.io/browse/DATAMONGO-1326) 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. – s7vr Dec 11 '18 at 15:06
  • yes I am using right version – Tejal Dec 12 '18 at 06:35
  • 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. – s7vr Dec 12 '18 at 13:28

1 Answers1

-1

The lookup was introduced in mongodb 3.4 please upgrade your dB

Vipul Pandey
  • 1,507
  • 11
  • 20
  • now i upgraded my version to v4.0.2 then also i am getting same error – Tejal Dec 10 '18 at 06:50
  • can you please share your mongodb debug log – Vipul Pandey Dec 10 '18 at 07:09
  • vipul i have modified query but still getting error – Tejal Dec 10 '18 at 07:24
  • its an issue in Spring framework if you are using spring-boot then please upgrade to 2.0 https://github.com/WoLpH/mongo-hacker/commit/5fdf310fae3ac7053e9e4e8a2ab88b42f2c68158 or use this AggregationOptions aggregationOptions=new AggregationOptions(false,false,200); and add this option at last i.e Aggregation.newAggregation(match1, lookupOperation).withOptions(aggregationOptions); – Vipul Pandey Dec 10 '18 at 08:09
  • i am not using spring boot – Tejal Dec 10 '18 at 08:16
  • then use the second as described in above – Vipul Pandey Dec 10 '18 at 08:39