[英]Mongodb 2 collection with collection array
I have two collections, Collection B has a general setting, the second collections has an ID stored in the field from B in the field, it needs to join them see the result.我有两个collections,Collection B有一个通用的设置,第二个collections有一个ID存储在字段B的字段中,需要加入他们看看结果。
Collection A集合 A
{
"_id": {
"$oid": "602ece156f898fc3a6da0440"
},
"name": "Atlas Adventures 2021",
"setup": [{
"_id": {
"$oid": "602ece156f898fc3a6da0441"
},
"typ": {
"$oid": "602eca46ad462fc07836d5e2"
},
"cena": 100,
"kapacita": 100
}, {
"_id": {
"$oid": "602ece156f898fc3a6da0442"
},
"typ": {
"$oid": "602eca59ad462fc07836d5e3"
},
"cena": 50,
"kapacita": 200
}],
} }
Collection B集合 B
{
{
"_id": {
"$oid": "602eca46ad462fc07836d5e2"
},
"nazev": "12 - 14 let - bez rozlišení\t",
"zkratka": "A",
"__v": 0,
"slozeniMaximum": 2,
"slozeniMinimum": 2,
},
{
"_id": {
"$oid": "602eca59ad462fc07836d5e3"
},
"zkratka": "B",
"nazev": "15-17 let",
"__v": 0,
"slozeniMaximum": 2,
"slozeniMinimum": 2,
}
}
Need需要
{
{
"_id": {
"$oid": "602eca46ad462fc07836d5e2"
},
"nazev": "12 - 14 let - bez rozlišení\t",
"zkratka": "A",
"slozeniMaximum": 2,
"slozeniMinimum": 2,
"cena": 50,
"kapacita": 200,
},
{
"_id": {
"$oid": "602eca59ad462fc07836d5e3"
},
"zkratka": "B",
"nazev": "15-17 let",
"slozeniMaximum": 2,
"slozeniMinimum": 2,
"cena": 50,
kapacita": 200,
}
}
But I do not know how.但是我不知道怎么做。 I've tried aggregation and pipeline, but I'm doing something wrong.
我已经尝试过聚合和管道,但我做错了。
According to this example agreggation , I created the following aggregation.根据这个示例agregation ,我创建了以下聚合。 There may be a problem with ObjectId.
ObjectId 可能有问题。
CollectionB.aggregate([{
$lookup: {
from: 'CollectionA',
let: { katId: '$_id' },
pipeline: [
{
$match: {
$expr: { $in: ['$setup.typ', '$$katId'] },
},
}
],
as: 'kategorie',
}
}
I still end in failure我仍然以失败告终
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.