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

公平

没有与嵌套的数组文件,只是想通过整理收集客户ID,然后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}}
])

对于两个字段,它工作正常,但是如果尝试添加“ productList.itemList.id”则不起作用,如下所示:

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
                }
            ]
        }
    ]
}
土生的

您无法直接对数组进行排序,首先需要展开(deconstruct),然后将应用排序,让我们一步一步看一下,

  • 产品列表
  • 解构数组($ unwind)
  • itemList
  • 解构数组($ unwind)
  • 按ID排序($ sort)
  • 重建数组($ group)
  • 按productId排序($ sort)
  • 重建productList($ group)
  • 按customerId排序($ sort)
  • $unwind解构productList数组
db.collection.aggregate([
  { $unwind: "$productList" },
  • $unwind解构productList.itemList数组
  { $unwind: "$productList.itemList" },
  • $sort通过productList.itemList.id升序
  { $sort: { "productList.itemList.id": 1 } },
  • $group通过所有3个主要级别的ID并重新构建itemList数组
  {
    $group: {
      _id: {
        _id: "$_id",
        customerId: "$customerId",
        productId: "$productList.productId"
      },
      itemList: { $push: "$productList.itemList" }
    }
  },
  • $sort通过productId升序
  { $sort: { "_id.productId": 1 } },
  • $group通过主要的2级ID并重新构建productList数组
  {
    $group: {
      _id: {
        _id: "$_id._id",
        customerId: "$_id.customerId"
      },
      productList: {
        $push: {
          productId: "$_id.productId",
          itemList: "$itemList"
        }
      }
    }
  },
  • $project 显示必填字段
  {
    $project: {
      _id: "$_id._id",
      customerId: "$_id.customerId",
      productList: 1
    }
  },
  • $sortcustomerId编号
  { $sort: { customerId: 1 } }
])

操场

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

MongoDB按数组元素对文档进行排序

来自分类Dev

MongoDB按数组中的值对文档进行排序

来自分类Dev

mongodb:使用字段名称的动态参数对嵌套数组进行排序

来自分类Dev

MongoDB使用sort和slice更新嵌套数组,不对desc进行排序

来自分类Dev

使用多个索引处的嵌套数组更新Mongoose文档

来自分类Dev

使用C#查询MongoDB嵌套数组文档

来自分类Dev

如何使用mongodb聚合转换嵌套数组中的文档

来自分类Dev

AngularJS在嵌套数组中插入数据并使用特定的嵌套数组对象进行排序

来自分类Dev

使用splice对嵌套数组进行排序的Javascript函数

来自分类Dev

mongoDB查询以嵌套数组查找文档

来自分类Dev

在MongoDB中更新嵌套数组文档

来自分类Dev

JavaScript:对嵌套数组进行排序

来自分类Dev

在mongodb中的多个嵌套数组中使用unwind

来自分类Dev

节点+ Mongodb +排序嵌套数组

来自分类Dev

节点+ Mongodb +排序嵌套数组

来自分类Dev

Mongodb:按数组最后一个元素中的值对文档进行排序

来自分类Dev

根据ElasticSearch中的嵌套子计数对文档进行排序

来自分类Dev

多个嵌套数组上的 MongoDB 聚合

来自分类Dev

如何使用Mongoose.js 3.8.20和MongoDB 2.6.5对文档进行排序?

来自分类Dev

在MongoDB中使用聚合框架按特定的嵌套字段值对文档进行计数

来自分类Dev

MongoDb查询,用于按值对文档进行排序

来自分类Dev

我可以使用嵌套sort()对嵌套数组进行排序吗?

来自分类Dev

我可以使用嵌套sort()对嵌套数组进行排序吗?

来自分类Dev

Mongo按数组中的值对文档进行排序

来自分类Dev

在Mongo中按嵌套数组中的文档值排序

来自分类Dev

使用Java对mongodb中的子文档数组进行排序

来自分类Dev

如何使用多个嵌套数组文档查找OneAndUpdate单字段

来自分类Dev

如何使用mongodp和php更新多个嵌套数组文档?

来自分类Dev

如何使用C#驱动程序删除mongodb文档中的嵌套数组元素

Related 相关文章

  1. 1

    MongoDB按数组元素对文档进行排序

  2. 2

    MongoDB按数组中的值对文档进行排序

  3. 3

    mongodb:使用字段名称的动态参数对嵌套数组进行排序

  4. 4

    MongoDB使用sort和slice更新嵌套数组,不对desc进行排序

  5. 5

    使用多个索引处的嵌套数组更新Mongoose文档

  6. 6

    使用C#查询MongoDB嵌套数组文档

  7. 7

    如何使用mongodb聚合转换嵌套数组中的文档

  8. 8

    AngularJS在嵌套数组中插入数据并使用特定的嵌套数组对象进行排序

  9. 9

    使用splice对嵌套数组进行排序的Javascript函数

  10. 10

    mongoDB查询以嵌套数组查找文档

  11. 11

    在MongoDB中更新嵌套数组文档

  12. 12

    JavaScript:对嵌套数组进行排序

  13. 13

    在mongodb中的多个嵌套数组中使用unwind

  14. 14

    节点+ Mongodb +排序嵌套数组

  15. 15

    节点+ Mongodb +排序嵌套数组

  16. 16

    Mongodb:按数组最后一个元素中的值对文档进行排序

  17. 17

    根据ElasticSearch中的嵌套子计数对文档进行排序

  18. 18

    多个嵌套数组上的 MongoDB 聚合

  19. 19

    如何使用Mongoose.js 3.8.20和MongoDB 2.6.5对文档进行排序?

  20. 20

    在MongoDB中使用聚合框架按特定的嵌套字段值对文档进行计数

  21. 21

    MongoDb查询,用于按值对文档进行排序

  22. 22

    我可以使用嵌套sort()对嵌套数组进行排序吗?

  23. 23

    我可以使用嵌套sort()对嵌套数组进行排序吗?

  24. 24

    Mongo按数组中的值对文档进行排序

  25. 25

    在Mongo中按嵌套数组中的文档值排序

  26. 26

    使用Java对mongodb中的子文档数组进行排序

  27. 27

    如何使用多个嵌套数组文档查找OneAndUpdate单字段

  28. 28

    如何使用mongodp和php更新多个嵌套数组文档?

  29. 29

    如何使用C#驱动程序删除mongodb文档中的嵌套数组元素

热门标签

归档