mongodb - mongo sorting with pagination -
i have need implement paging in mongodb
collection. example of document in collection
{ "_id" : "<mongo generated>", "category" : "abc", "createddate" : "<date of creation of document>", "markeddate" : "<date when document marked (marking activity done via application>" }
this requirement:
- i need retrieve documents in category "
abc
", in pages of 10 documents per page - the paging should done after documents sorted - have sort documents
markeddate
(descending) first,createddate
(descending) - a limit of 10 results should applied
- i should able skip page based on page number comes in input
i tried execute following query sort results using aggregate:
db.getcollection('testcollection').aggregate( {$match:{"category" : "abc"}}, {$sort:{markeddate: -1, createddate : -1} })
however, how page? can use skip + limit option, went through posts before posting question, , not recommended large data sets. anticipating collection have around 75000 documents.
also, since date stored in iso date format, see visibility @ level of seconds. may have 2 documents created @ same second.
what's best way page results without causing performance impact?
please advise.
how using sort function directly
db.testcollection.find({}).sort({createddate:-1}).skip(1).limit(10)
Comments
Post a Comment