查询mongodb中的对象数组数组

时间:2014-07-24 12:27:45

标签: arrays mongodb

我是mongodb的初学者,想要从一个对象数组中找到不同的对象。

我的文档如下:

    "_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] .dictionary

1 个答案:

答案 0 :(得分:0)

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

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

}  
])

结果:

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