0

In SO Question/ Answer there are still some parameters for basis of query.

I am looking for simple query like

p.find().limit(10).exec(function(err, qry){
  return qry;
}
Community
  • 1
  • 1
Sangram Singh
  • 7,161
  • 15
  • 50
  • 79

2 Answers2

2
var q = models.Post.find().sort('date', -1).limit(10);
q.execFind(function(err, posts) {
  // will be of length 10
});
LHH
  • 3,233
  • 1
  • 20
  • 27
  • Did this actually work for anybody ever? Didn't work for me and 6 months earlier there was a correct answer using `sort({'date': -1})` The right answer: https://stackoverflow.com/a/15081087/3462536 – NineToeNerd Jun 26 '17 at 19:56
2

The following worked for me with Mongoose 3.8.16:

var query = ModelName.find({}, null, {limit: 10, sort: {'epoch': -1}});
query.exec(function(err, docs) { ... });

This gives the last 10 entries in the DB sorted by the field epoch.

thameera
  • 9,168
  • 9
  • 37
  • 38