项目匹配数组的字段值

时间:2017-09-18 11:45:30

标签: mongodb aggregation-framework

我想更改文档结构,只在definition数组中显示与文档中idLanguage匹配的定义。我该怎么做?

包含definition数组的3个元素(三个不同的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"
        }
    ]
}

2 个答案:

答案 0 :(得分:1)

您可以使用$indexOfArray$arrayElemAt来匹配这些值。早期的问题表明你至少使用MongoDB 3.4,所以这应该不是问题:

db.collection.aggregate([
  { "$addFields": {
    "definition": {
      "$arrayElemAt": [
        "$definition.definition",
        { "$indexOfArray": [
          "$definition.idLanguage",
          "$idLanguage"
        }}
      ]
    }
  }}
])

"definition"的匹配位置由idLanguage(字段)从数组中提取。因此,您将使用在这些属性之间匹配的奇异值替换“数组”。

答案 1 :(得分: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"}
    }
  }
])