mongodb子文档如何排序?

淅淅沥沥
  • 157

{

    "_id" : ObjectId("609b3c4abcbf8cf50ecb8f1f"),
    "customer_code" : "1033497",
    "list" : [
            {
                    "word" : "虎狼之豹子",
                    "add_time" : ISODate("2021-05-12T02:25:39.026Z")
            },
            {
                    "word" : "虎狼之狗子",
                    "add_time" : ISODate("2021-05-12T02:25:54.515Z")
            },
            {
                    "word" : "广告",
                    "add_time" : ISODate("2021-05-12T02:30:18.339Z")
            },
            {
                    "word" : "拼多多",
                    "add_time" : ISODate("2021-05-12T02:30:18.339Z")
            }
    ]

}

现在我有这样的数据,想查询出来还是一样的结果,然后按着list的add_time倒叙,请问这个查询语句要怎么写呢?

回复
阅读 973
1 个回答

解决了,

 db.customer_sensitiveword.aggregate(
 {$match:{"customer_code":"1033497"}},
 {$unwind:"$list"},
 {$sort:{"list.add_time":-1}},
 {$project:{"_id":1,"customer_code":1,"list":1}},
 {$group:
     {"_id":"$_id","customer_code":{$first:"$customer_code"},"list":{$push:"$list"}}}
 ).pretty();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏