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:

  1. i need retrieve documents in category "abc", in pages of 10 documents per page
  2. the paging should done after documents sorted - have sort documents markeddate (descending) first, createddate (descending)
  3. a limit of 10 results should applied
  4. 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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -