简体   繁体   English

Mongodb - 多个集合

[英]Mongodb - multiple collections

I have 3 collections in Mongo (v5.0) and I want to get a subset of a collection using values in other collections.我在 Mongo (v5.0) 中有 3 个集合,我想使用其他集合中的值获取集合的子集。 I worked with options like $redact , $lookup but did not get the right results.我使用了$redact$lookup等选项,但没有得到正确的结果。 The most important part is array lookup in an array.最重要的部分是数组中的数组查找。

Users用户

[
{ "id": "a@example.com", "region": "US"},
{ "id": "b@example.com", "region": "EU"},
{ "id": "c@example.com", "region": "EU"},
{ "id": "d@example.com", "region": "US"},
]

Group团体

{ 
"HR": [ "a@example.com", "c@example.com" ],
"IT": [ "b@example.com", "d@example.com" ]
}

and I want to filter all users from a third collection called我想从名为的第三个集合中过滤所有用户

Rules规则

[ 
"group" : ["HR"],
"region" : ["US", "EU"]
]

so final outcome should be "All users in HR in US or EU region"所以最终结果应该是“美国或欧盟地区 HR 中的所有用户”

[
{ "id": "a@example.com", "region": "US"},
{ "id": "c@example.com", "region": "EU"},
]

Please help.请帮忙。

Unsure how the performance of this query is executed, but it results in the expected outcome:不确定此查询的性能如何执行,但会产生预期的结果:

  1. $lookup - Join users and groups collections: $lookup - 加入usersgroups集合:

    1.1. 1.1。 $filter - Filter the document from the result 1.1.1 with k is not _id (search for "HR" and "IT" only) and v array contains user_id via $in . $filter - 从结果1.1.1中过滤文档,其中k不是_id (仅搜索“HR”和“IT”)并且v数组通过$in包含user_id

    1.1.1. 1.1.1。 $objectToArray - Convert key-value pair into documents. $objectToArray - 将键值对转换为文档。

    1.2. 1.2. $unwind - Deconstruct the groups array into documents. $unwind - 将groups数组解构为文档。

    1.3. 1.3. $replaceWith - Replace the input documents with groups field. $replaceWith - 用groups字段替换输入文档。

  2. $lookup - Join with rules collection: $lookup - 加入rules集合:

    2.1. 2.1。 $filter - Filter the documents with: $filter - 过滤文档:

    2.1.1. 2.1.1。 $size & $setIntersection - There is at least 1 element with groups_dept (aka groups.k ) variable intersect with groups . $size & $setIntersection - 至少有 1 个元素与groups_dept (又名groups.k )变量与groups相交。

    2.1.2. 2.1.2. $in - region variable is in ( $in ) region array. $in - region变量在 ( $in ) region数组中。

  3. $match - Filter the document with rules is not an empty array. $match - 使用rules过滤文档不是空数组。 (Mean fulfill the rules criteria. (意味着满足rules标准。

  4. $unset - Remove rules and groups fields. $unset - 删除rulesgroups字段。

db.users.aggregate([
  {
    $lookup: {
      from: "groups",
      let: {
        user_id: "$id"
      },
      pipeline: [
        {
          $project: {
            groups: {
              $filter: {
                input: {
                  $objectToArray: "$$ROOT"
                },
                cond: {
                  $and: [
                    {
                      $not: {
                        $eq: [
                          "_id",
                          "$$this.k"
                        ]
                      }
                    },
                    {
                      $in: [
                        "$$user_id",
                        "$$this.v"
                      ]
                    }
                  ]
                }
              }
            }
          }
        },
        {
          $unwind: "$groups"
        },
        {
          $replaceWith: "$groups"
        }
      ],
      as: "groups"
    }
  },
  {
    $lookup: {
      from: "rules",
      let: {
        groups_dept: "$groups.k",
        region: "$region"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $gt: [
                    {
                      $size: {
                        $setIntersection: [
                          "$$groups_dept",
                          "$group"
                        ]
                      }
                    },
                    0
                  ]
                },
                {
                  $in: [
                    "$$region",
                    "$region"
                  ]
                }
              ]
            }
          }
        }
      ],
      as: "rules"
    }
  },
  {
    $match: {
      rules: {
        $not: {
          $eq: []
        }
      }
    }
  },
  {
    $unset: [
      "rules",
      "groups"
    ]
  }
])

Sample Mongo Playground示例 Mongo Playground

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM