Isak La Fleur
Isak La Fleur

Reputation: 4668

Project matching field value from array

I want to change the document structure and only display the definition in the definitionarray that matches the idLanguage in the document. How do I do that?

Example of a document with an definition array of 3 elements (three different id of 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"
        }
    ]
}

Upvotes: 2

Views: 825

Answers (2)

Shaishab Roy
Shaishab Roy

Reputation: 16805

You may want this if understood your problem correctly

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 to return only definition.definition

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

Upvotes: 1

Neil Lunn
Neil Lunn

Reputation: 151132

You can use $indexOfArray and $arrayElemAt to match the values. Earlier questions suggest you are using MongoDB 3.4 at least, so that should not be a problem:

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

That extracts from the array by "definition" ( the field ) at the matched position of idLanguage. So you would be replacing the "array" with the singular value that matches between those properties.

Upvotes: 1

Related Questions