2017-10-01 1 views
1

죄송합니다, 처음 mongo를 사용해보세요. 다음 데이터를 감안할 때 BsonArray를 포함한 쿼리 컬렉션

...

db.masterList.findOne() 
{ 
     "_id" : ObjectId("59d128805b19310ac8ab3fc2"), 
     "MasterDefinition" : { 
       "Location" : [ 
         "Whole House", 
         "Master Bedroom", 
         "Hallway 2" 
       ], 
       "DeviceType" : [ 
         "Receptacle", 
         "GFI", 
         "LED dimmer" 
       ], 
       "Style" : [ 
         "Decora", 
         "Standard" 
       ], 
       "Color" : [ 
         "White", 
         "Light Almond" 
       ] 
     } 
} 

은 어떻게 Color 배열의 내용을 검색 할 수 있습니까? 나는

["White","Light Almond"] 

같은 것을 어떻게 4 개 배열 직접 MasterDefintion에 종속 목록을 할 것으로 예상? aggregation framework, $project : 난 당신이 단순히

collection.aggregate({ 
    $project: { 
     "_id": 0, // exclude the "_id" field from the result 
     "result": "$MasterDefinition.Color" 
    } 
}) 

두 번째 부분은 문서는 여기에서 찾을 수 있습니다 (약간의 마법을 필요로 할 수있는,

["Location","DeviceType","Style","Color"] 

감사

첫 번째 부분에 대한
+0

는 "다음 컬렉션을 감안할 때 ..."당신이 당신의 질문에 일부 수집 세부 사항을 추가하려는 않은 : 다음은이 출력을 생성하는 ... ...

db.getCollection('masterList').aggregate([ // creates an array named "categories" from the attributes of the MasterDefinition sub document { $project: { categories: { $objectToArray: "$MasterDefinition" } } }, // projects on the keys of the "categories" array {$project: {'categories.k': 1}} ]) 

를 예를 들어입니까? – glytching

+0

질문을 (a) 지금까지 시도한 내용과 (b) ** 예상 ** 결과로 업데이트 할 수 있습니까? 찾고있는 출력에 대한 명확한 진술이 주어지면 간단하고 빠른 대답이 될 것입니다. – glytching

답변

0

를 볼 것으로 예상 , $objectToArray) :

collection.aggregate({ 
    $project: { 
     "temp": { 
      $objectToArray: "$MasterDefinition" // transform the "MasterDefinition" subdocument into an array 
     } 
    } 
}, { 
    $project:{ 
     "_id": 0, // do not include the "_id" field in the result - this is an optional step 
     "result": "$temp.k" // only get the keys (as in "k" fiels) from the array 
    } 
}) 
0

어떻게 Color 배열의 내용을 검색합니까?

{ 
    "_id" : ObjectId("59d128805b19310ac8ab3fc2"), 
    "MasterDefinition" : { 
     "Color" : [ 
      "White", 
      "Light Almond" 
     ] 
    } 
} 

가 어떻게 4 개 배열 직접 MasterDefintion에 종속 목록 않습니다 : 나는 위의 명령이 반환됩니다

// the first argument here is a filter, the second argument is a projection 
// since you specified no filter I have only included a projection 
// this projection tells MongoDB to return the Color subdocument 
// from within the MasterDefinition sub document 
db.getCollection('masterList').find({}, {'MasterDefinition.Color': 1}) 

[ "화이트", "빛 아몬드"] 같은 것을 기대? 나는이 조금 까다 롭습니다

"위치", "되는 DeviceType", "스타일"때문에 "컬러"가 아니다 [ "위치", "되는 DeviceType", "스타일", "색상"] 볼 것으로 예상 배열에있는 요소는 MasterDefinition 하위 문서의 특성 이름입니다. $objectToArray aggregation operator을 사용하여 이러한 속성 이름을 배열로 바꿀 수 있지만 결과 문서가 원하는 것과 정확하게 일치하지는 않습니다.

{ 
    "_id" : ObjectId("59d128805b19310ac8ab3fc2"), 
    "categories" : [ 
     { 
      "k" : "Location" 
     }, 
     { 
      "k" : "DeviceType" 
     }, 
     { 
      "k" : "Style" 
     }, 
     { 
      "k" : "Color" 
     } 
    ] 
} 
관련 문제