2017-09-18 4 views
2

문서 구조를 변경하고 문서의 idLanguage과 일치하는 definition 배열의 정의 만 표시하려고합니다. 어떻게해야합니까?배열에서 필드 값을 일치시키는 프로젝트

3 개 요소들의 어레이 definition 문서의 예 (idLanguage 세 가지 이드)

{ 
    "_id" : ObjectId("59bc29897d7934a6a7577ee0"), 
    "reference" : "FIIG=A23900 INC=62356", 
    "idTerm" : "0161-1#TM-218801#1", 
    "idLanguage" : "0161-1#LG-000002#1", 
    "statusTerm" : 0, 
    "idOrganisation" : "0161-1#OG-000194#1", 
    "idConcept" : "0161-1#01-000001#1", 
    "term" : "ZRYCHLOVAC ZçVERU, KE KULOMETU      ", 
    "definition" : [ 
     { 
      "_id" : ObjectId("59bc0bd77d7934a6a7243f05"), 
      "reference" : "FIIG=A23900 INC=62356", 
      "idDefinition" : "0161-1#DF-000001#1", 
      "idLanguage" : "0161-1#LG-000001#1", 
      "statusDefinition" : 0, 
      "idOrganisation" : "0161-1#OG-002462#1", 
      "definition" : "A metallic claw shaped pivoting item, designed to accelerate the weapon's recovery from recoil by assisting in realigning the breech with the barrel.", 
      "idConcept" : "0161-1#01-000001#1" 
     }, 
     { 
      "_id" : ObjectId("59bc29047d7934a6a7370782"), 
      "reference" : "FIIG=A23900 INC=62356", 
      "idDefinition" : "0161-1#DF-283090#1", 
      "idLanguage" : "0161-1#LG-000002#1", 
      "statusDefinition" : 0, 
      "idOrganisation" : "0161-1#OG-000194#1", 
      "definition" : "Kovov‡ otocn‡ p‡kov‡ polo_ka pro zrychlov‡n’ obnoven’ stavu zbrane pred zpetn_m r‡zem /v_strelem t’m, _e napom‡h‡ osov_mu ztoto_nen’ z‡vorn’ku /z‡veru s hlavn’.", 
      "idConcept" : "0161-1#01-000001#1" 
     }, 
     { 
      "_id" : ObjectId("59bc290b7d7934a6a73ce124"), 
      "reference" : "FIIG=A23900 INC=62356", 
      "idDefinition" : "0161-1#DF-668740#1", 
      "idLanguage" : "0161-1#LG-000005#1", 
      "statusDefinition" : 0, 
      "idOrganisation" : "0161-1#OG-000200#1", 
      "definition" : "Metalowy element wahliwy w ksztalcie szpona, przeznaczony do przyspieszenia powrotu broni po odrzucie poprzez wspomaganie ponownego ustawienia w linii zamka i lufy.", 
      "idConcept" : "0161-1#01-000001#1" 
     } 
    ] 
} 
+1

요청''definition.idLanguage'과 비교 idLanguage'을 반환이 이해하면 문제를 할 수 있습니다? –

+0

@ShaishabRoy correct. 당신의 답변에 감사드립니다! –

답변

1

$indexOfArray$arrayElemAt의 값과 일치하는 데 사용할 수있다. 이전 질문은 문제가되지 않습니다 적어도 있도록 MongoDB를 3.4을 사용하는 것이 좋습니다 :

idLanguage의 일치하는 위치에 "definition" (분야)에 의해 배열에서 추출
db.collection.aggregate([ 
    { "$addFields": { 
    "definition": { 
     "$arrayElemAt": [ 
     "$definition.definition", 
     { "$indexOfArray": [ 
      "$definition.idLanguage", 
      "$idLanguage" 
     }} 
     ] 
    } 
    }} 
]) 

. 따라서 "배열"을 이러한 속성 사이에 일치하는 단일 값으로 대체 할 수 있습니다.

1

당신은 제대로

db.collection.aggregate([ 
    { 
    $project: { 
     reference: 1, 
     defination: { 
     $filter: { 
      input: "$definition", 
      as: "elem", 
      cond: {$eq: ["$$elem.idLanguage", "0161-1#LG-000001#1"]} 
      // instead of "0161-1#LG-000001#1" you can use your variable 
     } 
     } 
    } 
    } 
]) 

또는definition.definition

db.collection.aggregate([ 
    {"$unwind": "$definition"}, 
    {"$match": {"definition.idLanguage": "0161-1#LG-000001#1"}}, 
    { 
    $group: { 
     _id: "$_id", 
     defination: {$push: "$definition.definition"} 
    } 
    } 
])