mongoose - How do I limit the number of returned items? -


mymodel.find({}, function(err, items){ console.log(items.length);    // big number }); 

how can limit returned items latest 10 items inserted?

in latest mongoose (3.8.1 @ time of writing), 2 things differently: (1) have pass single argument sort(), must array of constraints or 1 constraint, , (2) execfind() gone, , replaced exec() instead. therefore, mongoose 3.8.1 you'd this:

var q = models.post.find({published: true}).sort({'date': -1}).limit(20); q.exec(function(err, posts) {      // `posts` of length 20 }); 

or can chain that:

models.post .find({published: true}) .sort({'date': -1}) .limit(20) .exec(function(err, posts) {      // `posts` of length 20 }); 

Comments