$在mongodb查询中分别展开2个字段

我想放松2个领域,学校和家庭.

数据库结构就像;

{ 
    "id" : 1,
    "school" : [
        {
            "path" : "school1", 
            "code" : "code1", 
        }, 
        {
            "path" : "school2", 
            "code" : "code2", 
        }, 
        {
            "path" : "school3", 
            "code" : "code3",
        }, 
        {
            "path" : "school4", 
            "code" : "code4",
        } 
    ], 
    "home" : [
         {
            "path" : "home1", 
            "code" : "homeCode1",
        }, 
        {
            "path" : "home2", 
            "code" : "homeCode2",
        }, 
    ]
}

我想放松学校和家庭领域,让他们每个人都这样;

{ 
    "id" : 1,
    "school" : [
        {
            "path" : "school1", 
            "code" : "code1", 
        }
},
{ 
    "id" : 1,
    "school" : [
       {
            "path" : "school2", 
            "code" : "code2", 
        }
},
{ 
    "id" : 1,
    "school" : [
       {
            "path" : "school3", 
            "code" : "code3", 
        }
},
{ 
    "id" : 1,
    "school" : [
       {
            "path" : "school4", 
            "code" : "code4", 
        }
},
{ 
    "id" : 1,
    "home" : [
       {
            "path" : "home1", 
            "code" : "homeCode1", 
        }
},
{ 
    "id" : 1,
    "home" : [
       {
            "path" : "home2", 
            "code" : "homeCode2", 
        }
}   

我写的旨在获得上述格式的查询是;

db.collection.aggregate([
    {$unwind: "$school"},
    {$unwind: "$home"}
]).pretty()

但查询结果为对;

{
    "id" : 1,
    "school" : {
        "path" : "school1",
        "code" : "code1"
    },
    "home" : {
        "path" : "home1",
        "code" : "homeCode1"
    }
}
{
    "id" : 1,
    "school" : {
        "path" : "school1",
        "code" : "code1"
    },
    "home" : {
        "path" : "home2",
        "code" : "homeCode2"
    }
}
{
    "id" : 1,
    "school" : {
        "path" : "school2",
        "code" : "code2"
    },
    "home" : {
        "path" : "home1",
        "code" : "homeCode1"
    }
}
{
    "id" : 1,
    "school" : {
        "path" : "school2",
        "code" : "code2"
    },
    "home" : {
        "path" : "home2",
        "code" : "homeCode2"
    }
}
{
    "id" : 1,
    "school" : {
        "path" : "school3",
        "code" : "code3"
    },
    "home" : {
        "path" : "home1",
        "code" : "homeCode1"
    }
}
{
    "id" : 1,
    "school" : {
        "path" : "school3",
        "code" : "code3"
    },
    "home" : {
        "path" : "home2",
        "code" : "homeCode2"
    }
}
{
    "id" : 1,
    "school" : {
        "path" : "school4",
        "code" : "code4"
    },
    "home" : {
        "path" : "home1",
        "code" : "homeCode1"
    }
}
{
    "id" : 1,
    "school" : {
        "path" : "school4",
        "code" : "code4"
    },
    "home" : {
        "path" : "home2",
        "code" : "homeCode2"
    }
}

我怎样才能单独解开2个字段,而不是这个结果对格式?

最佳答案 这不是直接可能的.使用MongoDB 3.4,可能有一种解决方法. $facet可用于在同一文档的单个阶段中执行多组管道操作.它对你的情况很有用.

db.collection.aggregate({
    '$facet': {
        'school': [{
            '$unwind': '$school'
        }, {
            '$project': {
                _id: '1',
                school: 1
            }
        }],
        'home': [{
            '$unwind': '$home'
        }, {
            '$project': {
                _id: '1',
                home: 1
            }
        }]
    }
}, {
    '$project': {
        'schoolAndHome': {
            '$setUnion': ['$school', '$home']
        }
    }
}, {
    '$unwind': '$schoolAndHome'
}, {
    '$replaceRoot': {
        'newRoot': '$schoolAndHome'
    }
})
点赞