2017-09-18 5 views
2

文書の構造を変更し、文書内のidLanguageと一致するdefinition配列の定義のみを表示したいとします。それ、どうやったら出来るの?配列からのフィールド値を一致させるプロジェクト

3つの要素のdefinitionアレイを有する文書の一例(idLanguageの三つの異なるIDが):

{ 
    "_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

リクエストを'返すためにこれをしたいことがあり? –

+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 
     } 
     } 
    } 
    } 
]) 

OR正しく問題を理解している場合は、 `definition.idLanguage`と比較idLanguage`のみdefinition.definition

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