At this time I've something similar to this....

Item.find({}, function (docs) {
    for (var i = docs.length-15; i < docs.length; i++){

however it appears to be really slow. I am wishing to hurry up by doing something similar to...


Is possible? Does it help?


If you're only thinking about the very first (or last as with the situation of sorting with _id: -1) 15 documents then yes, setting a restriction around the totally makes sense. Restricting around the client side as with the first example implies that the database transmits each and every document towards the client, and so the client ignores every however the last 15.

However, the Mongoose syntax for indicating a restriction differs from the Mongo spend syntax, here things i think you would like:

Item.find().sort([['_id','descending']]).limit(15).each(function(doc) {

If I am not mistaken you are able to chain numerous actions on the Mongoose query, after which call each to transmit it and obtain each document from the result passed for your callback.

From things i can gleam in the actual source code and tests, since mongoose 1..14 the sort() parameter has transformed slightly to no more accept an assortment. In addition, you appear to call find() again around the actual Query object that is came back around the find call (plus looking out for the err object). So:-

  Item.find().sort('_id','descending').limit(15).find(function(err, doc) {

Hopefully might do the thing you need.