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

时间:2017-02-10 07:17:47

标签: mongodb mongodb-query aggregation-framework

我想$unwind 2个字段,schoolhome。 数据库结构就像;

{ 
    "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",
        }, 
    ]
}

我想要$unwind schoolhome字段,并将其中的每一个都视为;

{ 
    "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()

query成对出现;

{
    "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"
    }
}

我如何单独$unwind 2个字段,而不是这种结果对格式?

1 个答案:

答案 0 :(得分: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'
    }
})