如何使用MongoDB聚集管道将同一记录分为多组

发布于 2025-01-23 20:25:04 字数 1866 浏览 0 评论 0原文

我有两个收藏。

orgstructure(将其视为树结构)

示例文档:

{
    "id": "org1",
    "nodes": [
        {
            "nodeId": "root",
            "childNodes": ["child1"]
        },
        {
            "nodeId": "child1",
            "childNodes": ["child2"]
        },
        {
            "nodeId": "child2",
            "childNodes": []
        }
    ]
}

活动

示例文档:

[
    {
        "id":"A1",
        "orgUnit": "root"
    },
    {
        "id":"A2",
        "orgUnit": "child1"
    },
    {
        "id":"A3",
        "orgUnit": "child2"
    }
]

现在,我的期望是通过orgunit将其分组也考虑孩子节点。

在这里,我不想进行查找,我需要将一个访问者文档视为输入,以便我可以使用该文档构建某种条件,以至于查询将返回以下结果。

预期的结果

[
    {
        "_id": "root",
        "activities": ["A1","A2","A3"]
    },
    {
        "_id": "child1",
        "activities": ["A2","A3"]
    },
    {
        "_id": "child2",
        "activities": ["A3"]
    }
]

询问类似的汇总查询。

{
    "$group": {
        "_id": {
            "$switch": {
                "branches": [
                    {
                        "case": {"$in": ["$orgUnit",["root","child1","child2"]]},
                        "then": "root"
                    },
                    {
                        "case": {"$in": ["$orgUnit",["child1","child2"]]},
                        "then": "child1"
                    },
                    {
                        "case": {"$in": ["$orgUnit",["child2"]]},
                        "then": "child2"
                    }
                ],
                "default": null
            }
        }
    }
}

因此,我会提前

I have a two collections.

OrgStructure (visualise this as a tree structure)

Example Document:

{
    "id": "org1",
    "nodes": [
        {
            "nodeId": "root",
            "childNodes": ["child1"]
        },
        {
            "nodeId": "child1",
            "childNodes": ["child2"]
        },
        {
            "nodeId": "child2",
            "childNodes": []
        }
    ]
}

Activity

Example Document:

[
    {
        "id":"A1",
        "orgUnit": "root"
    },
    {
        "id":"A2",
        "orgUnit": "child1"
    },
    {
        "id":"A3",
        "orgUnit": "child2"
    }
]

Now my expectation is to group activities by orgUnit such a way that by considering the child nodes as well.

Here i don't want to do a lookup and i need to consider one OrgStructure document as an input, so that i can construct some condition using the document such a way that the query will return the below result.

Expected result

[
    {
        "_id": "root",
        "activities": ["A1","A2","A3"]
    },
    {
        "_id": "child1",
        "activities": ["A2","A3"]
    },
    {
        "_id": "child2",
        "activities": ["A3"]
    }
]

So im ecpecting an aggregate query something like this

{
    "$group": {
        "_id": {
            "$switch": {
                "branches": [
                    {
                        "case": {"$in": ["$orgUnit",["root","child1","child2"]]},
                        "then": "root"
                    },
                    {
                        "case": {"$in": ["$orgUnit",["child1","child2"]]},
                        "then": "child1"
                    },
                    {
                        "case": {"$in": ["$orgUnit",["child2"]]},
                        "then": "child2"
                    }
                ],
                "default": null
            }
        }
    }
}

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2025-01-30 20:25:04

您将需要2个步骤:

  1. 为递归查找创建另一个集合节点。原始orgstructure很难执行$ graphlookup
db.OrgStructure.aggregate([
  {
    "$unwind": "$nodes"
  },
  {
    "$replaceRoot": {
      "newRoot": "$nodes"
    }
  },
  {
    $out: "nodes"
  }
])
  1. persir $ graphlookup on nodes集合以获取所有子节点。执行$查找活动,并进行一些争吵。
db.nodes.aggregate([
  {
    "$graphLookup": {
      "from": "nodes",
      "startWith": "$nodeId",
      "connectFromField": "childNodes",
      "connectToField": "nodeId",
      "as": "nodesLookup"
    }
  },
  {
    "$lookup": {
      "from": "Activity",
      "let": {
        nodeId: "$nodesLookup.nodeId"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $in: [
                "$orgUnit",
                "$nodeId"
              ]
            }
          }
        },
        {
          $group: {
            _id: "$id"
          }
        }
      ],
      "as": "activity"
    }
  },
  {
    $project: {
      _id: "$nodeId",
      activities: "$activity._id"
    }
  }
])

这是 mongo Playground 供您参考。

You will need 2 steps:

  1. create another collection nodes for recursive lookup. The original OrgStructure is hard to perform $graphLookup
db.OrgStructure.aggregate([
  {
    "$unwind": "$nodes"
  },
  {
    "$replaceRoot": {
      "newRoot": "$nodes"
    }
  },
  {
    $out: "nodes"
  }
])
  1. Perform $graphLookup on nodes collection to get all child nodes. Perform $lookup to Activity and do some wrangling.
db.nodes.aggregate([
  {
    "$graphLookup": {
      "from": "nodes",
      "startWith": "$nodeId",
      "connectFromField": "childNodes",
      "connectToField": "nodeId",
      "as": "nodesLookup"
    }
  },
  {
    "$lookup": {
      "from": "Activity",
      "let": {
        nodeId: "$nodesLookup.nodeId"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $in: [
                "$orgUnit",
                "$nodeId"
              ]
            }
          }
        },
        {
          $group: {
            _id: "$id"
          }
        }
      ],
      "as": "activity"
    }
  },
  {
    $project: {
      _id: "$nodeId",
      activities: "$activity._id"
    }
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文