2014-07-24 52 views
0

我是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

回答

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 
}