我没有找到排序修饰符的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]);
当前回答
自2020年10月起,为了解决您的问题,您应该将.exec()添加到调用中。不要忘记,如果你想在调用之外使用这些数据,你应该在异步函数内部运行类似这样的东西。
let post = await callQuery();
async function callQuery() {
return Post.find().sort(['updatedAt', 1].exec();
}
其他回答
其他人为我工作,但这个做到了:
Tag.find().sort('name', 1).run(onComplete);
更新
如果这让人困惑,还有更好的记录;在猫鼬手册中查看查找文档和查询如何工作。如果您想使用fluent api,您可以通过不提供find()方法的回调来获得查询对象,否则您可以指定如下所述的参数。
原始
给定一个模型对象,根据model文档,它是如何在2.4.1中工作的:
Post.find({search-spec}, [return field array], {options}, callback)
搜索规范需要一个对象,但您可以传递null或空对象。
第二个参数是一个字符串数组的字段列表,所以你可以提供['field','field2']或null。
第三个参数是作为对象的options,它包括对结果集进行排序的能力。您可以使用{sort: {field: direction}},其中field是字符串fieldname test(在您的情况下),direction是一个数字,其中1是升序,-1是降序。
最后一个参数(callback)是接收查询返回的docs集合的回调函数。
Model.find()实现(在这个版本中)执行滑动分配属性来处理可选参数(这让我感到困惑!):
Model.find = function find (conditions, fields, options, callback) {
if ('function' == typeof conditions) {
callback = conditions;
conditions = {};
fields = null;
options = null;
} else if ('function' == typeof fields) {
callback = fields;
fields = null;
options = null;
} else if ('function' == typeof options) {
callback = options;
options = null;
}
var query = new Query(conditions, options).select(fields).bind(this, 'find');
if ('undefined' === typeof callback)
return query;
this._applyNamedScope(query);
return query.find(callback);
};
HTH
这就是我如何在mongoose.js 2.0.4中获得sort工作
var query = EmailModel.find({domain:"gmail.com"});
query.sort('priority', 1);
query.exec(function(error, docs){
//...
});
猫鼬v5.x.x
按升序排序
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) { ... });
详情:https://mongoosejs.com/docs/api.html#query_Query-sort
自2020年10月起,为了解决您的问题,您应该将.exec()添加到调用中。不要忘记,如果你想在调用之外使用这些数据,你应该在异步函数内部运行类似这样的东西。
let post = await callQuery();
async function callQuery() {
return Post.find().sort(['updatedAt', 1].exec();
}