开发者

Reversing the .each() order (mongodb)

开发者 https://www.devze.com 2023-03-01 17:27 出处:网络
using mongodb with mongoose: My current code returns the correct docs but sends them to the client in the wrong order. I tried adding another sort command after the .limit() to rev开发者_StackOverflo

using mongodb with mongoose:

My current code returns the correct docs but sends them to the client in the wrong order. I tried adding another sort command after the .limit() to rev开发者_StackOverflow社区erse this but it hasn't been working well. Any ideas to make this happen within the db call instead of extra code to reverse the order?

Item.find().sort('_id','descending').limit(40).each(function(err, doc) {
    if(doc != null){
        client.send(JSON.stringify(doc));       
    }
});


Depending on the actual context of your mongodb query try this (meteor client.js):

Item.find({}, { sort: { _id: -1 } })

or in db shell that code:

Item.find({}).sort({'_id': -1});


how about this?

var orderedList = new Array();
Item.find().sort('_id','descending').limit(40).each(function(err, doc) {
    orderedList.push(doc);
});

for (var i=orderedList.length; i>=0; i--){
   orderedList[i].doYourThang..
}

Try: JQuery .each() backwards for reverse() plugin

0

精彩评论

暂无评论...
验证码 换一张
取 消