简体   繁体   English

查询mongodb中对象数组的数组

[英]querying array of array of objects in mongodb

I'm a beginner in mongodb and want to find the distinct objects from an array of array of object. 我是mongodb的初学者,想从对象数组中查找不同的对象。

my document looks like this : 我的文档看起来像这样:

    "_class" : "com.fico.ifm.translation.domain",

    "_id" : ObjectId("53a2bccae4b0200bdf7e7bc2"),

    "screens" : [ 
        {
            "_id" : "systemParametersInvestigation",
            "dictionary" : [ 
                {
                    "locale" : "en-US",
                    "parameterizedValue" : "Edit close investigation configuration",
                    "key" : "CLOSE_INVESTIGATION_UPDATE_ALLOWED"
                }, 
                {
                    "locale" : "pt-BR",
                    "parameterizedValue" : "",
                    "key" : "CLOSE_INVESTIGATION_UPDATE_ALLOWED"
                }, 
                {
                    "locale" : "nl-NL",
                    "parameterizedValue" : "",
                    "key" : "CLOSE_INVESTIGATION_UPDATE_ALLOWED"
                }, 
                {
                    "locale" : "en-US",
                    "parameterizedValue" : "Close investigation update setting",
                    "key" : "CLOSE_INVESTIGATION_UPDATE_ALLOWED_DESCRIPTION"
                }, 
                {
                    "locale" : "pt-BR",
                    "parameterizedValue" : "",
                    "key" : "CLOSE_INVESTIGATION_UPDATE_ALLOWED_DESCRIPTION"
                }
]

}

 {
            "_id" : "adminRoles",

            "dictionary" : [ 
                {
                    "locale" : "en-US",
                    "parameterizedValue" : "Add all",
                    "key" : "ADD_ALL"
                }, 
                {
                    "locale" : "pt-BR",
                    "parameterizedValue" : "",
                    "key" : "ADD_ALL"
                }, 
                {
                    "locale" : "nl-NL",
                    "parameterizedValue" : "",
                    "key" : "ADD_ALL"
                }, 
                ]

I want to find distinct "key" in ,say, screens[i].dictionary 我想在屏幕上找到独特的“键” [i] .dictionary

db.yourcoll.aggregate([
{
    $unwind:"$screens"
},{
    $unwind:"$screens.dictionary"
},{    
    $group: {
        _id:"$_id",
        distinctKey:{$addToSet:"$screens.dictionary.key"}
    }
},{

    $project: {
        "_id" : 0,
        "distinctKey":1
    }

}  
])

Result: 结果:

{
    "result" : [ 
        {
            "distinctKey" : [ 
                "CLOSE_INVESTIGATION_UPDATE_ALLOWED_DESCRIPTION", 
                "CLOSE_INVESTIGATION_UPDATE_ALLOWED"
            ]
        }
    ],
    "ok" : 1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM