Product.find({
userId: data.id
},null,{ $sort :{ createdOn : -1}},function(err, products) {...});
我希望按照降序排列createdOn,但是没有得到期望的结果。
注意:createdOn是一个时间戳,我正在使用mongodb 1.3.19。
请帮忙!
你非常接近了。我猜你正在使用Mongoose(看起来是这样的)。
正确的语法应该是:
Product.find({userId: data.id}, null)
.sort('-createdOn')
.exec(function(err, products) { ... });
请查看查询对象的文档以获取更多信息。
Product.find({
userId: data.id
},null,{ sort :{ createdOn : -1}},function(err, products) {...});
我只需要将$从$sort中移除,现在它运行得很好:)