Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用聚合管道在MongoDB中将功能从一个集合添加到另一个集合_Javascript_Mongodb_Mongodb Query_Aggregation - Fatal编程技术网

Javascript 使用聚合管道在MongoDB中将功能从一个集合添加到另一个集合

Javascript 使用聚合管道在MongoDB中将功能从一个集合添加到另一个集合,javascript,mongodb,mongodb-query,aggregation,Javascript,Mongodb,Mongodb Query,Aggregation,我有两个与另一个相关的集合用户和组。一个用户只能在一个组中,而一个组可以包含多个用户 用户中的文档A { _id: 1234, name: "John Doe" } { _id: 2345, name: "Jane Roe" } 用户中的文档B { _id: 1234, name: "John Doe" } { _id: 2345, name: "Jane Roe" } 文档G分组 { _id: 3456, name: "A Group", m

我有两个与另一个相关的集合
用户
组。一个用户只能在一个组中,而一个组可以包含多个用户

用户中的文档A

{
  _id: 1234,
  name: "John Doe"
}
{
  _id: 2345,
  name: "Jane Roe"
}
用户中的文档B

{
  _id: 1234,
  name: "John Doe"
}
{
  _id: 2345,
  name: "Jane Roe"
}
文档G分组

{
  _id: 3456,
  name: "A Group",
  members: [ObjectId("1234"), ObjectId("2345")]
}
现在,我想使用集合
users
上的聚合管道将字段
\u group
添加到每个用户以进行进一步处理。添加的字段应包含用户所属组的ID

文档A的结果

{
  _id: 1234,
  name: "John Doe",
  _group: ObjectId("3456")
}
文档B的结果

{
  _id: 2345,
  name: "Jane Roe",
  _group: ObjectId("3456")
}

我真的不知道从哪里开始,以及如何以我描述的方式组合这两个集合。

这应该可以解决问题():

测试数据:

// users collection
[
    {
      _id: ObjectId("5a934e000102030405000001"),
      name: "John Doe"
    },
    {
      _id: ObjectId("5a934e000102030405000002"),
      name: "Jane Roe"
    }
]

// groups collection
[
    {
      _id: 100,
      name: "A Group",
      members: [
        ObjectId("5a934e000102030405000001"),
        ObjectId("5a934e000102030405000002")
      ]
    }
]
db.users.aggregate([
// join the two collections
  {
    $lookup: {
      "from": "groups",
      "localField": "_id",
      "foreignField": "members",
      "as": "membersInfo"
    }
  },
// unwind the membersInfo array
  {
    $unwind: "$membersInfo"
  },
  {
    $project: {
      "_id": {
        $cond: {
          "if": {
            $in: [
              "$_id",
              "$membersInfo.members" // replace _id field based on the members
            ]
          },
          "then": "$_id",
          "else": "No group"
        }
      },
      "name": 1, // mantain this field
      "_group": "$membersInfo._id" // create _group field using the _id of groups collection
    }
  }
])
[
  {
    "_group": 100,
    "_id": ObjectId("5a934e000102030405000001"),
    "name": "John Doe"
  },
  {
    "_group": 100,
    "_id": ObjectId("5a934e000102030405000002"),
    "name": "Jane Roe"
  }
]
查询:

// users collection
[
    {
      _id: ObjectId("5a934e000102030405000001"),
      name: "John Doe"
    },
    {
      _id: ObjectId("5a934e000102030405000002"),
      name: "Jane Roe"
    }
]

// groups collection
[
    {
      _id: 100,
      name: "A Group",
      members: [
        ObjectId("5a934e000102030405000001"),
        ObjectId("5a934e000102030405000002")
      ]
    }
]
db.users.aggregate([
// join the two collections
  {
    $lookup: {
      "from": "groups",
      "localField": "_id",
      "foreignField": "members",
      "as": "membersInfo"
    }
  },
// unwind the membersInfo array
  {
    $unwind: "$membersInfo"
  },
  {
    $project: {
      "_id": {
        $cond: {
          "if": {
            $in: [
              "$_id",
              "$membersInfo.members" // replace _id field based on the members
            ]
          },
          "then": "$_id",
          "else": "No group"
        }
      },
      "name": 1, // mantain this field
      "_group": "$membersInfo._id" // create _group field using the _id of groups collection
    }
  }
])
[
  {
    "_group": 100,
    "_id": ObjectId("5a934e000102030405000001"),
    "name": "John Doe"
  },
  {
    "_group": 100,
    "_id": ObjectId("5a934e000102030405000002"),
    "name": "Jane Roe"
  }
]
结果:

// users collection
[
    {
      _id: ObjectId("5a934e000102030405000001"),
      name: "John Doe"
    },
    {
      _id: ObjectId("5a934e000102030405000002"),
      name: "Jane Roe"
    }
]

// groups collection
[
    {
      _id: 100,
      name: "A Group",
      members: [
        ObjectId("5a934e000102030405000001"),
        ObjectId("5a934e000102030405000002")
      ]
    }
]
db.users.aggregate([
// join the two collections
  {
    $lookup: {
      "from": "groups",
      "localField": "_id",
      "foreignField": "members",
      "as": "membersInfo"
    }
  },
// unwind the membersInfo array
  {
    $unwind: "$membersInfo"
  },
  {
    $project: {
      "_id": {
        $cond: {
          "if": {
            $in: [
              "$_id",
              "$membersInfo.members" // replace _id field based on the members
            ]
          },
          "then": "$_id",
          "else": "No group"
        }
      },
      "name": 1, // mantain this field
      "_group": "$membersInfo._id" // create _group field using the _id of groups collection
    }
  }
])
[
  {
    "_group": 100,
    "_id": ObjectId("5a934e000102030405000001"),
    "name": "John Doe"
  },
  {
    "_group": 100,
    "_id": ObjectId("5a934e000102030405000002"),
    "name": "Jane Roe"
  }
]

所以你想更新收藏!你的数据库版本是什么?我不想修改原始集合,我只需要在聚合过程中进行合并以进行进一步处理。被接受的答案正是我想要的。