我没有找到排序修饰符的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()。排序({createdAt:“降序”});
其他回答
自2020年10月起,为了解决您的问题,您应该将.exec()添加到调用中。不要忘记,如果你想在调用之外使用这些数据,你应该在异步函数内部运行类似这样的东西。
let post = await callQuery();
async function callQuery() {
return Post.find().sort(['updatedAt', 1].exec();
}
从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
});
Post.find().sort({updatedAt: 1});
这就是我如何在mongoose.js 2.0.4中获得sort工作
var query = EmailModel.find({domain:"gmail.com"});
query.sort('priority', 1);
query.exec(function(error, docs){
//...
});