文档数组中的MongoDB组和计数对象

p5fdfcr1  于 2023-05-28  发布在  Go
关注(0)|答案(2)|浏览(202)

我需要在一个对象中对数组进行分组和计数。我试过很多方法,但都没有解决办法。
我有一个这样的mongodb文档:

[
    {
        "id" : 1,
        "name" : "A",
        "connected" : [
            {
                "iid" : 1,
                "risk" : "high"
            },
            {
                "iid" : 2,
                "risk" : "high"
            },
            {
                "iid" : 3,
                "risk" : "low"
            }
        ]
    }, 
    {
        "id" : 2,
        "name" : "B",
        "connected" : [
            {
                "iid" : 4,
                "risk" : "medium"
            },
            {
                "iid" : 5,
                "risk" : "medium"
            },
            {
                "iid" : 6,
                "risk" : "low"
            }
        ]
    },
    {
        "id":3,
        "name":"C"
    }
]

我想要一个查询,它会给予我这样的结果:

[
    {
        "id":1,
        "name":"A",
        "count": {
            "high" : 2,
            "low" : 1
        }
    },
    {
        "id":2,
        "name":"B",
        "count": {
            "medium" : 2,
            "low" : 1
        }
    },
    {
        "id":3,
        "name":"C"
    }
]

我在“$addFields”聚合查询中尝试了“$group”,但它不排除“$group”。

jucafojl

jucafojl1#

您可以在聚合管道中执行以下操作:

  1. $unwindconnected阵列
  2. $group乘以idconnected.risk以获得计数
  3. $groupid将结果放入{k:,v:}
    1.使用$arrayToObject获取所需的表单
db.collection.aggregate([
  {
    "$unwind": "$connected"
  },
  {
    // group by raw id and risk to get the count first
    $group: {
      _id: {
        id: "$id",
        risk: "$connected.risk"
      },
      name: {
        $first: "$name"
      },
      count: {
        $sum: 1
      }
    }
  },
  {
    // regroup the counts by putting them back to an array
    $group: {
      _id: "$_id.id",
      name: {
        $first: "$name"
      },
      count: {
        "$addToSet": {
          k: "$_id.risk",
          v: "$count"
        }
      }
    }
  },
  {
    // convert the result array back to the form you want
    "$project": {
      name: 1,
      count: {
        "$arrayToObject": "$count"
      }
    }
  }
])

这里是Mongo playground供您参考。

ia2d9nvy

ia2d9nvy2#

感谢@ray,我已经找到了解决方案,并添加了一些自定义选项,如;

  • 在结果中获取connected对象
  • count字段中添加total计数。

如果有人需要帮助解决这些问题,可以查看此参考:
https://mongoplayground.net/p/gRIOVj4A8Bh
结果是这样的:

[
    {
        "id": 1,
        "name": "A",
        "connected": [
            {
                "iid": 1,
                "risk": "high"
            },
            {
                "iid": 2,
                "risk": "high"
            },
            {
                "iid": 3,
                "risk": "low"
            }
        ],
        "count": {
            "high": 2,
            "low": 1,
            "total": 3
        }
    },
    {
        "id": 2,
        "name": "B",
        "connected": [
            {
                "iid": 4,
                "risk": "medium"
            },
            {
                "iid": 5,
                "risk": "medium"
            },
            {
                "iid": 6,
                "risk": "low"
            }
        ],
        "count": {
            "low": 1,
            "medium": 2,
            "total": 3
        }
    },
    {
        "id": 3,
        "name": "C",
        "count": {
            "total": 0
        }
    }
]

如果connected对象不需要,可以在查询结束时将其添加到$unset列表中。

相关问题