假设我在Mongoose中运行这个查询:
Room.find({}, (err,docs) => {
}).sort({date:-1});
这行不通!
假设我在Mongoose中运行这个查询:
Room.find({}, (err,docs) => {
}).sort({date:-1});
这行不通!
当前回答
短期解决方案:
const query = {}
const projection = {}
const options = { sort: { id: 1 }, limit: 2, skip: 10 }
Room.find(query, projection, options).exec(function(err, docs) { ... });
其他回答
这个适合我。
`Post.find().sort({postedon: -1}).find(function (err, sortedposts){
if (err)
return res.status(500).send({ message: "No Posts." });
res.status(200).send({sortedposts : sortedposts});
});`
看看这是否有助于>如何排序猫鼬?
也请阅读这个> http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order
这里所有的答案实际上都是正确的,但是我写我的答案是为了说明,如果你在模型中没有一个名为“date”的字段,或者你在创建模型时在选项中传递了选项:timestamps: true,那么有时写“-date”或date: -1将不起作用。如果你正在使用timestamps: true,那么你需要输入:sort({createdAt: -1}),这样就可以工作了。
正确答案是:
Blah.find({}).sort({date: -1}).execFind(function(err,docs){
});
ES6解决方案与Koa。
async recent() {
data = await ReadSchema.find({}, { sort: 'created_at' });
ctx.body = data;
}