Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose skip doesn't skip docs

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.

like image 388
Itsik Mauyhas Avatar asked Sep 19 '26 13:09

Itsik Mauyhas


1 Answers

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(...);
like image 111
robertklep Avatar answered Sep 22 '26 11:09

robertklep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!