本文介绍了$ unwind mongodb查询中的2个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to $unwind 2 fields, school and home.database structure is like;

{
    "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字段,并将它们分别作为;

I wanted to $unwind school and home fields and get each of them as;

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

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

The query that I wrote which aims to get format above is;

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个字段,而不是这种结果对格式?

How can I $unwind 2 fields separately, rather than this resulted pair format?

推荐答案

这不可能直接实现.使用MongoDB 3.4,有可能的解决方法. $facet可用于在同一文档的单个阶段中执行一组以上的管道操作.这对您的情况很有用.

It is not directly possible. With MongoDB 3.4 there is a possible workaround. $facet can be used to execute more than one set of pipeline operations in a single stage on the same document. It can be useful for your case.

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'
    }
})

这篇关于$ unwind mongodb查询中的2个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 16:15