首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB嵌套聚合未找到内部集合

MongoDB嵌套聚合未找到内部集合
EN

Stack Overflow用户
提问于 2017-12-05 18:02:03
回答 0查看 36关注 0票数 0

我是第一次接触Stack和MongoDB。我已经根据其他帖子调整了代码,但找不到哪里出错了。我有三个收藏。微生物与活动(1对M)和活动与生物活性(1对M)相连。我想以数组的形式展示来自Microbe的巢直到Bio-Activities。

代码语言:javascript
复制
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}

这是我的代码:

代码语言:javascript
复制
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()

但是我得到了这个输出,它显示了空的“生物活动”。我哪里错了?

代码语言:javascript
复制
    {
    "_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" : [ ]
            }
    ]
    }

谢谢。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47650772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档