我没有找到排序修饰符的doc。唯一的洞见在单元测试中: spec.lib.query.js # L12
writer.limit(5).sort(['test', 1]).group('name')
但这对我不起作用:
Post.find().sort(['updatedAt', 1]);
我没有找到排序修饰符的doc。唯一的洞见在单元测试中: spec.lib.query.js # L12
writer.limit(5).sort(['test', 1]).group('name')
但这对我不起作用:
Post.find().sort(['updatedAt', 1]);
当前回答
Post.find().sort({updatedAt:1}).exec(function (err, posts){
...
});
其他回答
从Mongoose 3.8.x开始:
model.find({ ... }).sort({ field : criteria}).exec(function(err, model){ ... });
地点:
条件包括asc、desc、升序、降序、1、-1
注意:使用引号或双引号
使用“asc”,“desc”,“ascending”,“descent”,1或-1
更新:
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
});
这是我如何排序和填充的:
Model.find()
.sort('date', -1)
.populate('authors')
.exec(function(err, docs) {
// code here
})
解决方案:
posts.find().sort({field:1})
//升序和降序使用-1而不是1
在当前版本的mongoose(1.6.0)中,如果你只想按一列排序,你必须删除数组并直接将对象传递给sort()函数:
Content.find().sort('created', 'descending').execFind( ... );
我花了一些时间,才把它弄好:(