Eduardo de Luna
Eduardo de Luna

Reputation: 491

Mongoose: How to slice the entire query?

I'm looking for a way to get M documents out of a particular query, starting at the Nth document, without rendering the entire collection at the exec() callback and then splice an array from there. I'm well aware of .limit(x) which works fine and dandy to get from 0 to x, but to my knowledge there is no way I select where does the query start limiting the number of documents, something like limit(10) starting from 5.

I tried something like this:

Model.find().sort({creationDate: -1}).where("_id").splice([5,10]).exec(function(err, data) {
                if(err) res.send(502, "ERROR IN DB DATABASE");
                res.send(data);
            });

But the resulting data consists of the entire collection. Any ideas on how to achieve this?

Upvotes: 1

Views: 583

Answers (1)

Enrique Fueyo
Enrique Fueyo

Reputation: 3488

.skip is what you are looking for

Model.find(...).sort(...).skip(5).limit(10).exec(....)

Upvotes: 5

Related Questions