I'm a beginner in mongodb and want to find the distinct objects from an array of array of object.
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
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.