【问题标题】:Sorting timestamp in descending order in mongodb在mongodb中按降序对时间戳进行排序
【发布时间】:2015-02-20 14:40:07
【问题描述】:
Product.find({
            userId: data.id
        },null,{ $sort :{ createdOn : -1}},function(err, products) {...});

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

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

请帮忙!!

【问题讨论】:

  • 这看起来不像是正确的语法。它会产生任何结果吗?
  • 是的,所有产品都显示但未排序,我只想对这些产品进行排序

标签: node.js mongodb


【解决方案1】:
Product.find({
        userId: data.id
    },null,{ sort :{ createdOn : -1}},function(err, products) {...});

我只需要从 $sort 中删除 $ 它现在对我来说很好:)

【讨论】:

    【解决方案2】:

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

    正确的语法应该是:

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

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

    【讨论】:

    • 感谢您的回复,但我自己想通了 :)
    • 太棒了!那样总是更好,因为这意味着你会更好地记住它。
    猜你喜欢
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多