猿问

Mongodb 如何按照内嵌文档的某个字段排序?

现有数据如下

{
    "_id" : ObjectId("5992c90beeb45634df1c2be4"),
    "name" : "测试数据1",
    "status" : {
        "product" : [ 
            {
                "id" : ObjectId("59a37f7aef887a1d58b59f4f"),
                "sort" : 2
            }, 
            {
                "id" : ObjectId("59a37f7bef887a1d58b59f50"),
                "sort" : 54
            }, 
            {
                "id" : ObjectId("59a3801cef887a1d58b59f55"),
                "sort" : 66
            }
        ],
        "line":[]
    }
},
{
    "_id" : ObjectId("599e7dc07780bbfa9bee42a9"),
    "name" : "test数据2",
    "status" : {
        "product" : [ 
            {
                "id" : ObjectId("59a37f7aef887a1d58b59f4f"),
                "sort" : 1
            }, 
            {
                "id" : ObjectId("59a37f7bef887a1d58b59f50"),
                "sort" : 30
            }, 
            {
                "id" : ObjectId("59a3801cef887a1d58b59f55"),
                "sort" : 60
            }
        ],
        "line":[]
    }
}
......

我想要把整个集合按照status.product里的id=ObjectId("59a37f7aef887a1d58b59f4f")的数据对应的sort字段进行倒序排序,查询语句应该怎么写呢?

慕码人8056858
浏览 1344回答 1
1回答
随时随地看视频慕课网APP
我要回答