我没有找到排序修饰符的doc。唯一的洞见在单元测试中: spec.lib.query.js # L12

writer.limit(5).sort(['test', 1]).group('name')

但这对我不起作用:

Post.find().sort(['updatedAt', 1]);

当前回答

您可以对查询结果进行排序

Post.find()。排序({createdAt:“降序”});

其他回答

其他人为我工作,但这个做到了:

  Tag.find().sort('name', 1).run(onComplete);

更新:

Post.find().sort({'updatedAt': -1}).all((posts) => {
  // do something with the array of posts
});

Try:

Post.find().sort([['updatedAt', 'descending']]).all((posts) => {
  // do something with the array of posts
});

这是我如何得到排序工作在猫鼬2.3.0:)

// Find First 10 News Items
News.find({
    deal_id:deal._id // Search Filters
},
['type','date_added'], // Columns to Return
{
    skip:0, // Starting Row
    limit:10, // Ending Row
    sort:{
        date_added: -1 //Sort by Date Added DESC
    }
},
function(err,allNews){
    socket.emit('news-load', allNews); // Do something with the array of 10 objects
})
// Ascending with updatedAt field
Post.find().sort('updatedAt').exec((err, post) => {...});

// Descending with updatedAt field
Post.find().sort('-updatedAt').exec((err, post) => {...});

参考网址:https://mongoosejs.com/docs/queries.html

在当前版本的mongoose(1.6.0)中,如果你只想按一列排序,你必须删除数组并直接将对象传递给sort()函数:

Content.find().sort('created', 'descending').execFind( ... );

我花了一些时间,才把它弄好:(