我没有找到排序修饰符的文档。唯一的见解是在单元测试中: spec.lib.query.js#L12
writer.limit(5).sort(['test', 1]).group('name')
但这对我不起作用:
Post.find().sort(['updatedAt', 1]);
我没有找到排序修饰符的文档。唯一的见解是在单元测试中: spec.lib.query.js#L12
writer.limit(5).sort(['test', 1]).group('name')
但这对我不起作用:
Post.find().sort(['updatedAt', 1]);
在 Mongoose 中,可以通过以下任何一种方式进行排序:
    Post.find({}).sort('test').exec(function(err, docs) { ... });
    Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
    Post.find({}).sort({test: 1}).exec(function(err, docs) { ... });
    Post.find({}, null, {sort: {date: 1}}, function(err, docs) { ... });
这就是我在mongoose 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
})
从mongoose 3.8.x 开始:
model.find({ ... }).sort({ field : criteria}).exec(function(err, model){ ... });
在哪里:
criteria可以是asc, desc, ascending, descending, 1, 或-1
注意:使用引号或双引号
使用"asc", "desc", "ascending", "descending", 1, 或-1
更新:
Post.find().sort({'updatedAt': -1}).all((posts) => {
  // do something with the array of posts
});
尝试:
Post.find().sort([['updatedAt', 'descending']]).all((posts) => {
  // do something with the array of posts
});
mongoose v5.xx
按升序排序
Post.find({}).sort('field').exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'asc' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'ascending' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 1 }).exec(function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'asc' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'ascending' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 1 }}), function(err, docs) { ... });
按降序排序
Post.find({}).sort('-field').exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'desc' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: 'descending' }).exec(function(err, docs) { ... });
Post.find({}).sort({ field: -1 }).exec(function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'desc' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : 'descending' }}), function(err, docs) { ... });
Post.find({}, null, {sort: { field : -1 }}), function(err, docs) { ... });