在 mongodb 中按降序对时间戳进行排序

Sorting timestamp in descending order in mongodb

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

我想按 desc 顺序对 createdOn 进行排序,但没有得到想要的结果

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

请帮忙!!

你很接近。我假设您正在使用 Mongoose(看起来像)。

正确的语法应该是:

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

查看 documentation for the Query object 了解更多信息。

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

我只需要从 $sort 中删除 $ 它现在对我来说工作正常:)