Well the clear an obvious case here is to use .skip()
as a modifier along with .limit()
in order to implement "paging" of data:
collection.find({}, { "limit": 5, "skip": 5 * req.body.requestCount }, function
But better still if you are just processing in batches, just filter out the range you have already seen. The _id
field makes a nice identifier for this without other sorting. So on the first request:
var lastSeen = null;
collection.find(
{},
{ "limit": 5, "sort": { "_id": 1} },
function(err,docs) {
docs.forEach(function(doc) {
// do something
lastSeen = doc._id; // keep the _id
});
}
);
And the next time around after storing that "lastSeen" in something like a session variable ( or other loop construct where just processing batches ):
collection.find(
{ "_id": { "$gt": lastSeen },
{ "limit": 5, "sort": { "_id": 1} },
function(err,docs) {
docs.forEach(function(doc) {
// do something
lastSeen = doc._id; // keep the _id
});
}
);
So excluding all results less that the last _id
value seen.
With other sorting this is still possible, but you need to take note of both the last _id
seen and the last sorted value as well. Also keeping the _id
seen as a list since the last value change.
var lastSeenIds = [],
lastSeenValue = null;
collection.find(
{},
{ "limit": 5, "sort": { "other": 1, "_id": 1 } },
function(err,docs) {
docs.forEach(function(doc) {
// do something
if ( lastSeenValue != doc.other ) { // clear on change
lastSeenValue = doc.other;
lastSeenIds = [];
}
lastSeenIds.push(doc._id); // keep a list
});
}
);
Then on your next iteration with variables in place:
collection.find(
{ "_id": { "$nin": lastSeenIds }, "other": { "$gte": lastSeenValue } },
{ "limit": 5, "sort": { "other": 1, "_id": 1 } },
function(err,docs) {
docs.forEach(function(doc) {
// do something
if ( lastSeenValue != doc.other ) { // clear on change
lastSeenValue = doc.other;
lastSeenIds = [];
}
lastSeenIds.push(doc._id); // keep a list
});
}
);
That is a lot more efficient than "skipping" through the results that match the basic query condition.
skip()
method. – Brahui