How to achieve pagination in java mongodb aggregation

1,377 views
Skip to first unread message

goutham gowda

unread,
Dec 22, 2015, 9:46:06 PM12/22/15
to mongodb-user
This is my query

DBCollection coll = this.mongoTemplate.getCollection("profileInterest");
        DBObject project = new BasicDBObject("$project", new BasicDBObject("_id", 1).append("fromProfile", 1).append("toProfile", 1)
                .append("isContactNoViewed", 1).append("isShortListed", 1)
                .append("interestStatusTotal", new BasicDBObject("$size", "$interestStatus"))
                .append("interestStatus", new BasicDBObject("$slice", new Object[] { "$interestStatus", 1 })));
        DBObject match = new BasicDBObject("$match", new BasicDBObject("threeFavorites.id", new BasicDBObject("$ne", 100)));

        @SuppressWarnings("deprecation")
        AggregationOutput output = coll.aggregate(project, match);

Dwight Merriman

unread,
Dec 22, 2015, 9:49:37 PM12/22/15
to mongodb-user
you will typically use $skip and $limit in the pipeline to do pagination.

you can also use skip() and limit() in a find() operation if your query doesn't require any aggregation -- either will work.

goutham gowda

unread,
Dec 22, 2015, 10:14:30 PM12/22/15
to mongodb-user
Thanks for your reply @dwight , I have one more doubt how to get total records count for this aggregation. 
Reply all
Reply to author
Forward
0 new messages