How to limit the number of returned items?

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

How can I limit the returned items to only the last 10 items that have been inserted?

+93
mongoose
Apr 29 2018-11-11T00:
source share
7 answers

In the last mongoose (3.8.1 at the time of writing) you do two things differently: (1) you need to pass one sort () argument, which should be an array of constraints or just one constraint, and (2) execFind () disappeared and replaced by exec () instead. Therefore, using mongoose 3.8.1 you will do the following:

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

or you can link it just like this:

 models.Post .find({published: true}) .sort({'date': -1}) .limit(20) .exec(function(err, posts) { // `posts` will be of length 20 }); 
+155
Dec 15 '13 at 14:43
source share

Similarly, using .limit ():

 var q = models.Post.find({published: true}).sort('date', -1).limit(20); q.execFind(function(err, posts) { // `posts` will be of length 20 }); 
+17
Apr 29 '11 at 1:58 p.m.
source share

I'm a little lazy, so I like simple things:

 let users = await Users.find({}, null,{limit: 50}); 
+5
Dec 21 '18 at 22:08
source share
 models.Post.find({published: true}, {sort: {'date': -1}, limit: 20}, function(err, posts) { // 'posts' with sorted length of 20 }); 
+4
Feb 07 '17 at 10:07 on
source share

For some reason, I couldn't get this to work with the proposed answers, but I found another option using select, which worked for me:

 models.Post.find().sort('-date').limit(10).select('published').exec(function(e, data){ ... }); 

Perhaps api has changed? I am using version 3.8.19

+1
Dec 07 '14 at 6:47
source share

... additionally be sure to use:

 mongoose.Promise = Promise; 

This means that the promise of the mongoose is consistent with the promise of ES6. Without this addition, I got:

DeprecationWarning: Mongoose: mpromise (default mongoose promise library) is deprecated, add your own promise library instead: http://mongoosejs.com/docs/promises.html p>

+1
Sep 01 '17 at 17:15
source share

Find options

Search function parameters:

  1. "Object" conditions.
  2. [projection] "Object|String" optional return fields, see Query.prototype.select ()
  3. [options] "Object" optional, see Query.prototype.setOptions ()
  4. [call back] "Function"

How to limit

 const Post = require('./models/Post'); Post.find( { published: true }, null, { sort: { 'date': 'asc' }, limit: 20 }, function(error, posts) { if (error) return '${error} while finding from post collection'; return posts; // posts with sorted length of 20 } ); 

Additional Information

Mongoose allows you to request your collections in a variety of ways, such as: Official documentation

 // named john and at least 18 MyModel.find({ name: 'john', age: { $gte: 18 }}); // executes, passing results to callback MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {}); // executes, name LIKE john and only selecting the "name" and "friends" fields MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { }) // passing options MyModel.find({ name: /john/i }, null, { skip: 10 }) // passing options and executes MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {}); // executing a query explicitly var query = MyModel.find({ name: /john/i }, null, { skip: 10 }) query.exec(function (err, docs) {}); // using the promise returned from executing a query var query = MyModel.find({ name: /john/i }, null, { skip: 10 }); var promise = query.exec(); promise.addBack(function (err, docs) {}); 
0
Apr 30 '19 at 5:48
source share



All Articles