2

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

1 Answer 1

8

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.

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

7 Comments

I tried it. I got error - The method skip(int) is undefined
@Rajnishkatiyar-SIPL So the variable for skip is undefined! Surely that should be a clear error. The point I am making is that your basic calling sequence was incorrect.
I can assure you that is not an issue because I have tried like this also- skip( 0 ), limit( 5 )
@Rajnishkatiyar-SIPL You need to take more than a few seconds to response. Run the code. .skip(0) does not result in "undefined".
@BlakesSeven you are right in that, but mongodb java spring driver dont have skip and limit method like sort and group. I also have same problem with skip and limit function with aggregation.
|

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.