【发布时间】:2021-06-16 02:21:38
【问题描述】:
我需要帮助加入 MongoDB 中的两个集合。
我有两个系列:卡片和套牌。我需要加入它们,以便在卡片组文档中拥有卡片文档的属性。下面是每个集合的两个 JSON 格式的示例:
- 甲板
例子:
{
"_id": {
"$oid": "608edd53a8220000b2000882"
},
"name": "Izzet Phoenix",
"cards": {
"mainboard": [
{
"qty": "4",
"name": "Arclight Phoenix",
"id": "787de9ce-02c5-4a17-a88b-d38e83dbeb0b"
},
... a bunch of other cards in the same format (qty, name, id)
],
"sideboard": [
{
"qty": "2",
"name": "Abrade",
"id": "84319dfb-eaf7-4b98-8c4f-30f5e779591b"
},
... a bunch of other cards in the same format (qty, name, id)
]
},
... a bunch of other properties of the deck
}
- 卡片
例子:
{
"_id": {
"$oid": "608b60d6a40a0000c9017853"
},
"name": "Abrade",
"id": "84319dfb-eaf7-4b98-8c4f-30f5e779591b",
... a bunch of other properties of the card
}
请注意,decks 集合使用卡片的“id”字段作为参考,而不是“_id”字段。
这是我正在寻找的期望输出:
{
"_id": {
"$oid": "608edd53a8220000b2000882"
},
"name": "Izzet Phoenix",
"cards": {
"mainboard": [
{
"qty": "4",
"name": "Arclight Phoenix",
"id": "787de9ce-02c5-4a17-a88b-d38e83dbeb0b",
... add properties of the card Arclight Phoenix, with id = 787de9ce-02c5-4a17-a88b-d38e83dbeb0b
},
... a bunch of other cards
],
"sideboard": [
{
"qty": "2",
"name": "Abrade",
"id": "84319dfb-eaf7-4b98-8c4f-30f5e779591b",
... add properties of the card Abrade, with id 84319dfb-eaf7-4b98-8c4f-30f5e779591b
},
... a bunch of other cards
]
},
... a bunch of other properties of the deck
}
我尝试研究如何解决这个问题,我发现 MongoDB 有一个 $lookup 阶段,但我不熟悉管道和阶段的概念,这最终让我望而却步。
感谢所有帮助。
感谢您的宝贵时间!
【问题讨论】:
标签: mongodb