在MongoDB上加入多个集合

在MongoDB上加入多个集合,mongodb,mongodb-query,Mongodb,Mongodb Query,我想加入两个集合,然后再加入另一个集合。如何做到这一点 数据: 聊天 突击搜查: { "_id": ObjectId("5fd1ea22d605fede5818414e"), "expires": "2020-12-10T09:34:10.808Z", "invites_left": NumberInt("3"), "trainers&

我想加入两个集合,然后再加入另一个集合。如何做到这一点

数据:

聊天

突击搜查:

{
    "_id": ObjectId("5fd1ea22d605fede5818414e"),
    "expires": "2020-12-10T09:34:10.808Z",
    "invites_left": NumberInt("3"),
    "trainers": [
        {
            "_id": ObjectId("5fc375f7bf36c0adbd587578"),
            "username": "dsffdddsfs",
            "host": true
        },
        {
            "_id": ObjectId("5fd257e3faafc618f3b29b07"),
            "username": "prova2"
        }
    ],
}
培训师:

{
    "_id": ObjectId("5fd1e467d41457dc20a1f2f3"),
    "email": "test@test.com",
    "password": "1",
    "username": "dsffdddsfs",
    "country": "ES",
    "city": "Barcelona",
},
{
    "_id": ObjectId("5fd1e9acd605fede58184149"),
    "email": "test2@test.com",
    "password": "1",
    "username": "prova2",
    "country": "ES",
    "city": "Barcelona",
}
我想有一个这样的东西:

{
    "_id": ObjectId("5fd1ea22d605fede58184150"),
    "raid": ObjectId("5fd1ea22d605fede5818414e"),
    "description": "",
    "password": "",
    "created": ISODate("2020-12-10T09:28:02.661Z"),
    "messages": [ ],
    "trainers": [
        {
        "email": "test@test.com",
        "password": "1",
        "username": "dsffdddsfs",
        "country": "ES",
        "city": "Barcelona",
            "host": true
        },
        {
        "email": "test2@test.com",
            "password": "1",
            "username": "prova2",
            "country": "ES",
            "city": "Barcelona",
        }
    ],
}
因此,首先我必须加入与
raid
的聊天,然后是与
trainers
的结果(合并数据)。 到目前为止,我已经做到了:

db.chats.aggregate([
{
    $match: 
        {
            raid : ObjectId('5fd1ea22d605fede5818414e')
        }
},
{
    $lookup : 
        {
            from : "raids",
            localField : "raid",
            foreignField : "_id", 
            as : "FoundRaid"
        }
},
{
        $unwind: "$FoundRaid"
},
{
    $lookup : 
        {
            from : "FoundRaid.trainers",
            localField : "FoundRaid.username",
            foreignField : "username", 
            as : "FoundTrainerTOTAL"
        }
},
  {             // merfing not works here yet
    $addFields: {
      trainers: {
        $map: {
          input: "$trainers",
          as: "s",
          in: {
            $mergeObjects: [
              "$$s",
              {
                $first: {
                  $filter: {
                    input: "$FoundTrainer",
                    cond: {
                      $eq: [
                        "$$s.username",
                        "$$this.username"
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
]);
这给了我不正确/不完整的数据,如(缺少与培训师的连接):

如何做到这一点?如何使查找的“from”成为另一个
$lookup
的结果? 谢谢

  • $lookup
    带有
    raids
    集合
  • $unwind
    解构
    raid
    结果
  • $lookup
    使用
    培训师
    集合
  • $addFields
    要合并两个对象数组,
    $map
    要迭代
    训练器的循环
    数组,
    $reduce
    要迭代
    raid的循环。训练器
    数组并检查条件并获得匹配结果,返回到
    $map
    ,将使用
    $mergeObjects
    合并对象
  • 使用
    $$remove

db.chats.aggregate([
{
    $match: 
        {
            raid : ObjectId('5fd1ea22d605fede5818414e')
        }
},
{
    $lookup : 
        {
            from : "raids",
            localField : "raid",
            foreignField : "_id", 
            as : "FoundRaid"
        }
},
{
        $unwind: "$FoundRaid"
},
{
    $lookup : 
        {
            from : "FoundRaid.trainers",
            localField : "FoundRaid.username",
            foreignField : "username", 
            as : "FoundTrainerTOTAL"
        }
},
  {             // merfing not works here yet
    $addFields: {
      trainers: {
        $map: {
          input: "$trainers",
          as: "s",
          in: {
            $mergeObjects: [
              "$$s",
              {
                $first: {
                  $filter: {
                    input: "$FoundTrainer",
                    cond: {
                      $eq: [
                        "$$s.username",
                        "$$this.username"
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
]);
{
    "_id": ObjectId("5fd1ea22d605fede58184150"),
    "archived": false,
    "tipus": "raid",
    "raid": ObjectId("5fd1ea22d605fede5818414e"),
    "description": "",
    "password": "",
    "created": ISODate("2020-12-10T09:28:02.661Z"),
    "messages": [ ],
    "FoundRaid": {
        "_id": ObjectId("5fd1ea22d605fede5818414e"),
        "expires": "2020-12-10T09:34:10.808Z",
        "invites_left": NumberInt("3"),
        "trainers": [
            {
                "_id": ObjectId("5fc375f7bf36c0adbd587578"),
                "username": "dsffdddsfs",
                "host": true
            },
            {
                "_id": ObjectId("5fd257e3faafc618f3b29b07"),
                "username": "prova2"
            }
        ],
        "__v": NumberInt("2")
    },
    "FoundTrainerTOTAL": [ ],
    "trainers": null
}
db.charts.aggregate([
  {
    $lookup: {
      from: "raids",
      localField: "raid",
      foreignField: "_id",
      as: "raid"
    }
  },
  { $unwind: "$raid" },
  {
    $lookup: {
      from: "trainers",
      localField: "raid.trainers.username",
      foreignField: "username",
      as: "trainers"
    }
  },
  {
    $addFields: {
      raid: "$$REMOVE",
      trainers: {
        $map: {
          input: "$trainers",
          as: "t",
          in: {
            $mergeObjects: [
              "$$t",
              {
                host: {
                  $reduce: {
                    input: "$raid.trainers",
                    initialValue: null,
                    in: {
                      $cond: [
                        { $eq: ["$$this.username", "$$t.username"] },
                        "$$this.host",
                        "$$value"
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
])