假设我在Mongoose中运行这个查询:
Room.find({}, (err,docs) => {
}).sort({date:-1});
这不管用!
假设我在Mongoose中运行这个查询:
Room.find({}, (err,docs) => {
}).sort({date:-1});
这不管用!
Mongoose中的Sorting已经在发布过程中进化,因此其中一些答案不再有效.从4.1.x版Mongoose开始,date
字段的降序排序可以通过以下任一方式完成:
Room.find({}).sort('-date').exec((err, docs) => { ... });
Room.find({}).sort({date: -1}).exec((err, docs) => { ... });
Room.find({}).sort({date: 'desc'}).exec((err, docs) => { ... });
Room.find({}).sort({date: 'descending'}).exec((err, docs) => { ... });
Room.find({}).sort([['date', -1]]).exec((err, docs) => { ... });
Room.find({}, null, {sort: '-date'}, (err, docs) => { ... });
Room.find({}, null, {sort: {date: -1}}, (err, docs) => { ... });
对于升序排序,请省略字符串版本上的-
前缀,或使用1
、asc
或ascending
的值.