我是Stack和MongoDB的新手。我根据其他帖子调整了我的代码,但无法弄清楚我哪里出错了。我有三个系列。微生物与活动(1至M)和活动与生物活动(1至M)相关联。我想以微阵列的形式展示巢穴,直到阵列形式的Bio-Activities。
Microbe collection
{"_id": 1, item: "abc", species: 12, quantity: 2},
{"_id": 2, item: "jkl", species: 20, quantity: 1},
{"_id": 3}
Activities collection
{"_id": 1, code: "MON1003", item: "abc", type: "Monitor", repeat: 120, size: "27", resolution: "1920x1080"},
{"_id": 2, code: "MON1012", item: "abc", type: "Monitor", repeat: 85, size: "23", resolution: "1280x800"},
{"_id": 3, code: "MON1031", item: "jkl", type: "Monitor", repeat: 60, size: "21", resolution: "1920x1080"}
Bio-Activities collection
{"_id": 1, code: "MON1003", description: "bio 1", quantity: 120},
{"_id": 2, code: "MON1003", description: "bio 2", quantity: 80},
{"_id": 3, code: "MON1012", description: "bio 3", quantity: 60},
{"_id": 4, code: "MON1012", description: "bio 4", quantity: 70},
{"_id": 5, code: "MON1012", description: "bio 5", quantity: 170},
{"_id": 6, code: "MON1031", description: "bio 6", quantity: 270}
这是我的代码:
db.microbe.aggregate([
{
$lookup:
{
from: "activities",
localField: "item",
foreignField: "item",
as: "activities"
}
},{
$lookup:
{
from: "bioactivities",
localField: "code",
foreignField: "code",
as: "bioactivities"
}
},{
$unwind: "$activities"
},{
$group: {
_id: "$_id",
item: {$first: "$item"},
species: {$first: "$species"},
quantity: {$first: "$quantity"},
activities: {$push: {
id: "$activities._id",
code: "$activities.code",
item: "$activities.item",
type: "$activities.type",
repeat: "$activities.repeat",
size: "$activities.size",
bioactivities: "$bioactivities"
}
}
}
}
]).pretty()
但我得到的结果显示空的"生物活性"。我哪里出错了?
{
"_id" : 2,
"item" : "jkl",
"species" : 20,
"quantity" : 1,
"activities" : [
{
"id" : 3,
"code" : "MON1031",
"item" : "jkl",
"type" : "Monitor",
"repeat" : 60,
"size" : "21",
"bioactivities" : [ ]
}
]
}
{
"_id" : 1,
"item" : "abc",
"species" : 12,
"quantity" : 2,
"activities" : [
{
"id" : 1,
"code" : "MON1003",
"item" : "abc",
"type" : "Monitor",
"repeat" : 120,
"size" : "27",
"bioactivities" : [ ]
},
{
"id" : 2,
"code" : "MON1012",
"item" : "abc",
"type" : "Monitor",
"repeat" : 85,
"size" : "23",
"bioactivities" : [ ]
}
]
}
感谢。
答案 0 :(得分:0)
这是一个愚蠢的错误,我认为你忽略了它。
在您的第二个$lookup
中,localField
应该"activities.code"
这样,
{
$lookup:
{
from: "bioactivities",
localField: "activities.code",
foreignField: "code",
as: "bioactivities"
}
}
这将为您提供如下输出:
{
"_id" : 2.0,
"item" : "jkl",
"species" : 20.0,
"quantity" : 1.0,
"activities" : [
{
"id" : 3.0,
"code" : "MON1031",
"item" : "jkl",
"type" : "Monitor",
"repeat" : 60.0,
"size" : "21",
"bioactivities" : [
{
"_id" : 6.0,
"code" : "MON1031",
"description" : "bio 6",
"quantity" : 270.0
}
]
}
]
}
我希望这是你正在寻找的。 p>
答案 1 :(得分:0)
知道了。 “订单确实很重要”,引用了Mongo的视频。 解决方案:在我查找“生物活动”之前,我应该放松我的“活动”。这是代码。
db.microbe.aggregate([
{
$lookup:
{
from: "activities",
localField: "item",
foreignField: "item",
as: "activities"
}
},{
$unwind: "$activities"
},{
$lookup:
{
from: "bioactivities",
localField: "activities.code",
foreignField: "code",
as: "bioactivities"
}
},{
$group: {
_id: "$_id",
item: {$first: "$item"},
species: {$first: "$species"},
quantity: {$first: "$quantity"},
activities: {$push: {
id: "$activities._id",
code: "$activities.code",
item: "$activities.item",
type: "$activities.type",
repeat: "$activities.repeat",
size: "$activities.size",
bioactivities: "$bioactivities"
}
}
}
}
]).pretty()