想知道如何使用聚合管道将procedures个数组按type就地分组.

因此,将这个转变为:

[
    {
        "airport": "MSP",
        "country": "USA",
        "procedures": [
            {
                "name": "ABC1",
                "type": "DEP"
            },
            {
                "name": "JKL2",
                "type": "ARR"
            },
            {
                "name": "LMN3",
                "type": "DEP"
            },
            {
                "name": "PQR4",
                "type": "ARR"
            }
        ]
    },
    {
        "airport": "CMH",
        "country": "USA",
        "procedures": [
            {
                "name": "GHI4",
                "type": "ARR"
            },
            {
                "name": "RST3",
                "type": "ARR"
            },
            {
                "name": "TUV7",
                "type": "DEP"
            },
            {
                "name": "XYZ4",
                "type": "ARR"
            }
        ]
    }
]

进入这一阶段:

[
    {
        "airport": "MSP",
        "country": "USA",
        "procedures": {
            "ARR": [
                {
                    "name": "JKL2",
                    "type": "ARR"
                },
                {
                    "name": "PQR4",
                    "type": "ARR"
                }
            ],
            "DEP": [
                {
                    "name": "ABC1",
                    "type": "DEP"
                },
                {
                    "name": "LMN3",
                    "type": "DEP"
                },
            ]
        }
    },
    {
        "airport": "CMH",
        "country": "USA",
        "procedures": {
            "ARR": [
                {
                    "name": "GHI4",
                    "type": "ARR"
                },
                {
                    "name": "RST3",
                    "type": "ARR"
                },

                {
                    "name": "XYZ4",
                    "type": "ARR"
                }
            ],
            "DEP": [
                {
                    "name": "TUV7",
                    "type": "DEP"
                }
            ]
        }
    }
]

我已经走到了

db.collection.aggregate([
  {
    "$unwind": "$procedures"
  },
  {
    "$group": {
      _id: "$procedures.type",
      procedures: {
        $push: "$procedures"
      }
    }
  }
])

但它丢失了airport个 struct ,并将数据混合在一起:

[
  {
    "_id": "DEP",
    "procedures": [
      {
        "name": "ABC1",
        "type": "DEP"
      },
      {
        "name": "LMN3",
        "type": "DEP"
      },
      {
        "name": "TUV7",
        "type": "DEP"
      }
    ]
  },
  {
    "_id": "ARR",
    "procedures": [
      {
        "name": "JKL2",
        "type": "ARR"
      },
      {
        "name": "PQR4",
        "type": "ARR"
      },
      {
        "name": "GHI4",
        "type": "ARR"
      },
      {
        "name": "RST3",
        "type": "ARR"
      },
      {
        "name": "XYZ4",
        "type": "ARR"
      }
    ]
  }
]

推荐答案

  1. 第二阶段:$group × airportprocedures.type.

  2. 第三阶段:$groupairport,将具有kv个字段的对象添加到procedures数组中.

  3. 最后阶段:$project,通过$arrayToObject操作符将procedures数组转换为键-值对来修饰输出文档.

db.collection.aggregate([
  {
    "$unwind": "$procedures"
  },
  {
    "$group": {
      _id: {
        type: "$procedures.type",
        airport: "$airport"
      },
      country: {
        $first: "$country"
      },
      procedures: {
        $push: "$procedures"
      }
    }
  },
  {
    "$group": {
      _id: "$_id.airport",
      country: {
        $first: "$country"
      },
      procedures: {
        $push: {
          k: "$_id.type",
          v: "$procedures"
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      airport: "$_id",
      country: "$country",
      procedures: {
        $arrayToObject: "$procedures"
      }
    }
  }
])

Demo @ Mongo Playground

Mongodb相关问答推荐

MongoDB—基于数组中同一文档中的另一个字段更新字段

数组字段包含其他字段的数组值

MongoDB与合并对象聚合

Mongo Aggregate管道-另一个集合中的多个查找

MongoDB 根据最新日期查询多个不同的值

mongodb.将文档分组在数组中,对它们进行评分计数和求和并添加新字段

找到一个用户,然后使用 MongoDB 根据他们的总分获得他们的排名

通过 _id 更新一个文档(无效的 BSON 字段名称 _id)

如何在 Mongo 聚合管道中获取限制之前的计数

哪个库最适合用于带有 Scala 的 MongoDB?

我怎样才能排序空值在 mongodb 中是最后排序的?

MongoDB:设置 Windows 服务

如果我在 MongoDB 上使用 LINQ,为什么会失go 性能?

在 MongoDB 中合并两个集合

mongo - Ruby连接问题

使用 Jackson 与 Java Mongo DBObject 进行高效 POJO 映射

在mongoose中查询虚拟属性

如何通过mongoose更新 mongodb 中的对象?

是否可以使用聚合框架对 MongoDB 中的 2 个字段求和?

如何在 Windows 上停止 mongodb 服务器?