Reputation: 201
I have a need to implement paging in my mongodb
collection. This is an example of a document in my collection
{
"_id" : "<Mongo Generated>",
"category" : "abc",
"createdDate" : "<Date of creation of the document>",
"markedDate" : "<Date when the document is marked
(marking is an activity that is done via my application>"
}
This is my requirement:
abc
", however in pages of 10 documents per page markedDate
(Descending) first, then createdDate
(descending)I tried to execute the following query to sort results using aggregate:
db.getCollection('testcollection').aggregate(
{$match:{"category" : "abc"}},
{$sort:{markedDate: -1, createdDate : -1}
})
However, how do I page? I can use a skip + limit option, however I went through some posts before posting this question, and it is not recommended for large data sets. I am anticipating that my collection with have around 75000 documents.
Also, since my date is stored in ISO date format, I see that the visibility is only at the level of seconds. I may have 2 documents created at the same second.
What's the best way to page my results without causing a performance impact?
Please advise.
Upvotes: 2
Views: 3431
Reputation: 2386
How about using sort function directly
db.testcollection.find({}).sort({createdDate:-1}).skip(1).limit(10)
Upvotes: 2