嵌套文档上的MongoDB $ lookup [英] MongoDB $lookup on nested document

查看:102
本文介绍了嵌套文档上的MongoDB $ lookup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongo的新手,并且在以下方面苦苦挣扎.我有2个集合,结构如下.为了我的一生,我不知道该如何对学校收藏进行查询.在阅读其他文章时,我肯定会使用ObjectId作为参考以及外部字段.

I'm new to mongo and struggling mightily with the following. I have 2 collections structured as below. For the life of me, I can't figure out how to do a $lookup on the school collection. Reading other posts, I'm definitely using ObjectId for the reference as well as the foreign field.

下面是我的结构:

校友:

{
    "_id": "john",
    "items": [
        {
            "name": "John",
            "items": [
                {
                    "school": ObjectId("56de35ab520fc05b2fa3d5e4"),
                    "grad": true
                },
                {
                    "school": ObjectId("56de35ab520fc05b2fa00000"),
                    "grad": false
                }
            ]
        },
        {
            "name": "Johnny"
            // notice no nested items, this doc should still be included in result
        },
        {
            "name": "Jon",
            "items": [
                {
                    "school": ObjectId("56de35ab520fc05b2fa11111"),
                    "grad": false
                }
            ]
        }
     ]
}

学校

{
    _id: ObjectId("56de35ab520fc05b2fa3d5e4"),
    name: "Some University",
    street: "ABC Boulevard"
}

我想要得到什么:

{
    "_id": "john",
    "items": [
        {
            "name": "John",
            "items": [
                {
                    "school": ObjectId("56de35ab520fc05b2fa3d5e4"),
                    "grad": true,
                    "schoolInfo":     {
                        _id: ObjectId("56de35ab520fc05b2fa3d5e4"),
                        name: "Some University",
                        street: "ABC Boulevard"
                    }
                },
                {
                    "school": ObjectId("56de35ab520fc05b2fa00000"),
                    "grad": true,
                    "schoolInfo":     {
                        _id: ObjectId("56de35ab520fc05b2fa00000"),
                        name: "Another University",
                        street: "123 Boulevard"
                    }
                }
            ]
        },
        {
            name: "Johnny"
        },
        {
            "name": "Jon",
            "items": [
                {
                    "school": ObjectId("56de35ab520fc05b2fa11111"),
                    "grad": true,
                    "schoolInfo":     {
                        _id: ObjectId("56de35ab520fc05b2fa11111"),
                        name: "Some University",
                        street: "ABC Boulevard"
                    }
                }
            ]
         }
     ]
}

我尝试不成功的查询:

db.alumni.aggregate([
      {$match: {_id: 'john'}}, 
      {$lookup: {
                from: 'schools', 
                localField: 'items.items.school', 
                foreignField: '_id', 
                as: 'schoolInfo'}}
 ])

任何帮助将不胜感激!

推荐答案

在这种情况下,需要在聚合框架中很好地使用$ unwind和$ project

in this case there is required a nice play with $unwind and $project in aggregation framework

请参见以下内容:

db.alumni.aggregate([
    {$match: {_id: 'john'}},
    {$unwind:"$items"},
    {$unwind:"$items.items"},
    {$lookup: {
        from: 'schools', 
        localField: 'items.items.school', 
        foreignField: '_id', 
        as: 'schoolInfo'}},
    {$unwind:"$schoolInfo"},
    {$project:{
        "_id":1,
        "items":[{
            "name":"$items.name",
            "items":[{
            "school":"$schoolInfo._id"    ,
            "grad":"$items.items.grad"    ,
            "schoolInfo":"$schoolInfo"
            }]
        }]            
    }}
]).pretty()

查看其工作原理-尝试从查询中删除聚合阶段并检查文档结构.

to see how it works - try removing aggregation stages from query and check document structure.

这篇关于嵌套文档上的MongoDB $ lookup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆