在MongoDB中按时间戳降序排序

3
Product.find({
            userId: data.id
        },null,{ $sort :{ createdOn : -1}},function(err, products) {...});

我希望按照降序排列createdOn,但是没有得到期望的结果。

注意:createdOn是一个时间戳,我正在使用mongodb 1.3.19。

请帮忙!


这看起来不像是正确的语法。它会产生任何结果吗? - undefined
是的,所有的产品都显示出来了,但是没有排序,我只想要对这些产品进行排序。 - undefined
2个回答

3

你非常接近了。我猜你正在使用Mongoose(看起来是这样的)。

正确的语法应该是:

Product.find({userId: data.id}, null)
    .sort('-createdOn')
    .exec(function(err, products) { ... });

请查看查询对象的文档以获取更多信息。


谢谢回复,但我自己解决了 :) - undefined
太棒了!这样做总是更好,因为这意味着你会记得得更牢固。 - undefined

3
Product.find({
        userId: data.id
    },null,{ sort :{ createdOn : -1}},function(err, products) {...});

我只需要将$从$sort中移除,现在它运行得很好:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接