简体   繁体   中英

Mongodb Java driver use limit in aggregation query

Problem

Query is working fine but not limit and skip, it is fetching all records at once.

Kindly suggest what I am doing wrong.

MongoDB Collection

 {  
      "_id" : ObjectId("559666c4e4b07a176940c94f"),     
      "postId" : "559542b1e4b0108c9b6f390e",    
     "user" : {         
                 "userId" : "5596598ce4b07a176940c943",         
                 "displayName" : "User1",       
                 "username" : "user1",      
                 "image" : ""   
      },    
      "postFor" : {         
                     "type": "none",
                      "typeId" : ""     
      },    
     "actionType" : "like",     
     "isActive" : 1,
     "createdDate" : ISODate("2015-07-03T10:41:07.575Z"),   
     "updatedDate" : ISODate("2015-07-03T10:41:07.575Z") 
}

Java driver Query

 Aggregation aggregation = newAggregation(                                

      match(Criteria.where("isActive").is(1).and("user.userId").in(feedUsers)),
      group("postId")
      .last("postId").as("postId")
      .last("postFor").as("postFor")
      .last("actionType").as("actionType")
      .last("isActive").as("isActive")
      .last("user").as("user")
      .last("createdDate").as("createdDate")
      .last("updatedDate").as("updatedDate"),
      sort(Sort.Direction.DESC, "createdDate")
    );
aggregation.skip( skip );
aggregation.limit( limit );
AggregationResults<UserFeedAggregation> groupResults =
mongoOps.aggregate(aggregation, SocialActionsTrail.class, UserFeedAggregation.class);
return groupResults.getMappedResults();

Thanks

Aggregation pipelines are "sequential" in operation. These are not like .find() operations where .sort() .limit() .skip() are "modifiers" to the query operation:

Aggregation aggregation = newAggregation(                               
    match(Criteria.where("isActive")
        .is(1).and("user.userId").in(feedUsers)),
    group("postId")
        .last("postId").as("postId")
        .last("postFor").as("postFor")
        .last("actionType").as("actionType")
        .last("isActive").as("isActive")
        .last("user").as("user")
        .last("createdDate").as("createdDate")
        .last("updatedDate").as("updatedDate"),
    sort(Sort.Direction.DESC, "createdDate"),
    skip( skip ),
    limit( limit )
);

Unless you define the operations in "sequence" then the pipeline does not know the order of execution. So define the pipeline as a whole.


A basic example:

Aggregation aggregation = newAggregation(
    group("postId"),
    skip(1),
    limit(1)
);

System.out.println(aggregation)

Outputs a perfect pipeline:

{ 
    "aggregate" : "__collection__" ,
    "pipeline" : [ 
        { "$group" : { "_id" : "$postId" } },
        { "$skip" : 1 },
        { "$limit" : 1 }
    ]
}

See $skip and $limit in the core documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM