我没有找到排序修饰符的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]);
当前回答
在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) { ... });
其他回答
这是我如何排序和填充的:
Model.find()
.sort('date', -1)
.populate('authors')
.exec(function(err, docs) {
// code here
})
从4开始。X排序方法已经改变。如果您正在使用>4.x。尝试使用以下任何一种方法。
Post.find({}).sort('-date').exec(function(err, docs) { ... });
Post.find({}).sort({date: -1}).exec(function(err, docs) { ... });
Post.find({}).sort({date: 'desc'}).exec(function(err, docs) { ... });
Post.find({}).sort({date: 'descending'}).exec(function(err, docs) { ... });
Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
Post.find({}, null, {sort: '-date'}, function(err, docs) { ... });
Post.find({}, null, {sort: {date: -1}}, function(err, docs) { ... });
Post.find().sort({updatedAt: 1});
在当前版本的mongoose(1.6.0)中,如果你只想按一列排序,你必须删除数组并直接将对象传递给sort()函数:
Content.find().sort('created', 'descending').execFind( ... );
我花了一些时间,才把它弄好:(
其他人为我工作,但这个做到了:
Tag.find().sort('name', 1).run(onComplete);