在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"
]
}
}
}
}
]
}
}
}
}
}
])