A bit odd in query performance ... I need to run a query that runs the total number of documents, and can also return a result set that can be limited and biased.
So, I have 57 documents in total, and the user wants 10 documents to be shifted by 20.
I can think of two ways to do this, first a query for all 57 documents (returned as an array), and then using array.slice returns the documents they need. The second option is to run 2 queries, the first of which uses the count count mongo method, and then run the second query using mongo native $ limit and $ skip aggregators.
Which, in your opinion, will be better scaled? Doing all this in a single query or running two separate ones?
Edit:
// 1 query var limit = 10; var offset = 20; Animals.find({}, function (err, animals) { if (err) { return next(err); } res.send({count: animals.length, animals: animals.slice(offset, limit + offset)}); }); // 2 queries Animals.find({}, {limit:10, skip:20} function (err, animals) { if (err) { return next(err); } Animals.count({}, function (err, count) { if (err) { return next(err); } res.send({count: count, animals: animals}); }); });
mongodb mongoose
leepowell Dec 18 '12 at 15:03 2012-12-18 15:03
source share