2017-10-11 7 views
0

이 두 문서 아래 고려 발생 카운트 :그룹 배열 요소가

{ "name" : "test1", "source" : ["Apple","Cherry"], "dest" : ["Banana", "Durian"]} 
{ "name" : "test2", "source" : ["Melon","Fig"], "dest" : ["Apple"]}] 

입력

["Apple","Orange","Fig"] 

예상 출력

{ "_id" : "Apple", nameList : ["test1", "test2"] } 
{ "_id" : "Fig", nameList : ["test2"] } 
{ "_id" : "Orange", nameList : [] } 

집계

db.getCollection<name>.aggregate([ 
    { "$match" : <some_matching_conditions>}, 
    { "$project" : 
      { "$project" : { "input_array" : { "$literal" : ["Apple","Orange","Fig"]} }}, 
      { "obj" : {"$setUnion" : ["$source", "$dest"]}}, 
    }, 
    { "$unwind" : "$aray"}, 
    { "$match" : { "aray" : { "$in" : "$obj"}}}, 
    { "$group" : <grouping_conditons>} 
]) 
+0

오류 : "errmsg": "예외 : 잘못된 쿼리 : BadValue $에 배열이 필요합니다" – Bala

답변

0

아래 집계를 시도 할 수 있습니다.

db.collection_name.aggregate([ 
    { 
    "$project": { 
     "name": 1, 
     "farray": { 
     "$setIntersection": [ 
      { 
      "$concatArrays": [ 
       "$source", 
       "$dest" 
      ] 
      }, 
      input_array 
     ] 
     } 
    } 
    }, 
    { 
    "$unwind":"$farray" 
    }, 
    { 
    "$group": { 
     "_id": "$farray", 
     "namelist": { 
     "$push": "$name" 
     } 
    } 
    } 
])