I want to iterate over my collection that contains ~31k docs.
each time I want to query to return 100 docs using skip to start from the first doc and return the next 100 and so on.
I am getting the skip index from the request:
find: function (req, res) {
var name = "node"
var limit = 100;
console.log(req);
var query = {};
query = req.query;
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
console.log("skip typeof : " + typeof(Number(query.skip)));
var Collection = getCollection(name);
Collection.find(query).skip(Number(query.skip)).limit(limit).toArray(function(err, docs) {
console.log(docs);
res.send(docs);
});
});
the console logs shows that query.skip : 1, 101 , 201 ... so the problem must be in my query:
Collection.find(query).skip(Number(query.skip)).limit(limit).toArray(function(err, docs) {
console.log(docs);
res.send(docs);
});
but the docs that the query returns are the same for each request:
req 1 [{nid : 4033},{nid:4501}]
req 2 [{nid : 4033},{nid:4501}]
skip value is Number :
skip typeof : number
Thanks for your help.
My guess is that query.skip is a string, and MongoDB wants it to be a number:
Collection.find({}).skip(Number(query.skip)).limit(...)
EDIT: apparently, you're passing query to find() as well, which won't work if skip is also a property (as MongoDB will consider that to be a query field).
Try this:
var skip = Number(query.skip);
delete query.skip;
Collection.find(query).skip(skip).limit(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With