MongoDb使用多个嵌套数组对文档进行排序

MongoDb使用多个嵌套数组对文档进行排序,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,有一个具有嵌套数组的文档,只需按customerId、productList.productId和productList.itemList.id升序对集合进行排序。MongoDb版本是3.0.14 到目前为止,我已经尝试了这个方法,但查询没有按预期对集合进行排序: db.file_old.find({}, { customerId: 1, "productList.productId": 1, "productList

有一个具有嵌套数组的文档,只需按customerId、productList.productId和productList.itemList.id升序对集合进行排序。MongoDb版本是3.0.14

到目前为止,我已经尝试了这个方法,但查询没有按预期对集合进行排序:

db.file_old.find({}, {
        customerId: 1,
        "productList.productId": 1,
        "productList.itemList.id": 1
    })
    .sort({
        customerId: 1,
        productList: 1,
        "productList.itemList": 1
    })
并尝试以下聚合框架:

db.file_old.aggregate([
    {"$unwind": "$productList"} ,
    {"$sort": {"customerId": 1, "productList.productId": 1}}
])
db.file_old.aggregate([
    {"$unwind": "$productList"} ,
    {"$sort": {"customerId": 1, "productList.productId": 1,  "productList.itemList.id": 1}}
])
对于两个字段,它可以正常工作,但如果尝试添加“productList.itemList.id”不起作用,如下所示:

db.file_old.aggregate([
    {"$unwind": "$productList"} ,
    {"$sort": {"customerId": 1, "productList.productId": 1}}
])
db.file_old.aggregate([
    {"$unwind": "$productList"} ,
    {"$sort": {"customerId": 1, "productList.productId": 1,  "productList.itemList.id": 1}}
])
收集结构:

{
    "_id" : ObjectId("5f33cc2a1e84082968132324"),
    "customerId" : 2196,
    "productList" : [
        {
            "productId" : 7531,
            "itemList" : [
                {
                    "id" : 144
                },
                {
                    "id" : 145
                }
            ]
        },
        {
            "productId" : 7534,
            "itemList" : [
                {
                    "id" : 1244
                },
                {
                    "id" : 1243
                },
                {
                    "id" : 1245
                },
                {
                    "id" : 1242
                }
            ]
        }
    ]
},{
    "_id" : ObjectId("5f33cc2a1e84082968132326"),
    "customerId" : 2201,
    "productList" : [
        {
            "productId" : 101201,
            "itemList" : [
                {
                    "id" : 863
                },
                {
                    "id" : 865
                },
                {
                    "id" : 862
                }
            ]
        },
        {
            "productId" : 7537,
            "itemList" : [
                {
                    "id" : 982
                },
                {
                    "id" : 1002
                },
                {
                    "id" : 896
                }
            ]
        }
    ]
}

您不能直接对数组进行排序,首先需要展开(解构),然后应用排序,让我们一步一步地看

  • 产品列表
  • 解构数组($unwind)
  • 项目列表
  • 解构数组($unwind)
  • 按id排序($sort)
  • 重新构造数组($group)
  • 按productId排序($sort)
  • 重新构造productList($group)
  • 按客户ID排序($sort)
  • $unwind
    解构
    productList
    数组
  • $unwind
    解构
    productList.itemList
    数组
  • $sort
    productList.itemList.id
    升序排序
  • $group
    通过所有3个主要级别的ID重新构建
    项目列表
    数组
  • $sort
    productId
    升序排序
  • $group
    按主2级ID重新构造
    productList
    数组
  • $project
    显示必填字段
  • $sort
    by
    customerId
    id

我尝试了你的第一篇文章,没有正确排序
productList.productId
@turivishal有没有办法不展开就进行排序?我遇到了一个prblm tht,我有三层嵌套数组,我需要sort@varman我不知道,但会检查,我认为对于查询来说,放松是昂贵的,可能会影响性能和速度,会询问社区并向您更新。当然。谢谢,当我们使用大量数据时,放松是昂贵的。如果你得到帮助,请告诉我,如果我得到帮助,我会的。@varman是的,如果你使用MongoDb处理数百万数据,很多东西都很昂贵。
  { $sort: { "productList.itemList.id": 1 } },
  {
    $group: {
      _id: {
        _id: "$_id",
        customerId: "$customerId",
        productId: "$productList.productId"
      },
      itemList: { $push: "$productList.itemList" }
    }
  },
  { $sort: { "_id.productId": 1 } },
  {
    $group: {
      _id: {
        _id: "$_id._id",
        customerId: "$_id.customerId"
      },
      productList: {
        $push: {
          productId: "$_id.productId",
          itemList: "$itemList"
        }
      }
    }
  },
  {
    $project: {
      _id: "$_id._id",
      customerId: "$_id.customerId",
      productList: 1
    }
  },
  { $sort: { customerId: 1 } }
])