Gyaneshwar Pardhi
Gyaneshwar Pardhi

Reputation: 471

Select specific objects from array of object inside another array of object from mongodb collection

Consider below json object as attribute collection

{
    "_id" : ObjectId("5912f2d9d5dcab2bf5ed4572"),
    "name" : "design",
    "hybris_name" : "design",
    "type" : "predefin_values",
    "options" : [
        {
            "english_text" : "Animal Print",
            "arabic_text" : "مركب",
            "template_id" : [
                1,
                2,
                4
            ]
        },
        {
            "englis_text" : "Straight",
            "arabic_text" : "بوي فريند",
            "template_id" : [
                1,
                3
            ]
        }
    ]
}
{
    "_id" : ObjectId("59130618d5dcab2bf5ed4573"),
    "name" : "fit",
    "hybris_name" : "",
    "fit" : "predefin_values",
    "options" : [
        {
            "englis_text" : "Slim",
            "arabic_text" : "واسع",
            "template_id" : [
                1,
                3
            ]
        },
        {
            "englis_text" : "Straight",
            "arabic_text" : "بوي فريند",
            "template_id" : [
                1
            ]
        },
        {
            "englis_text" : "bend",
            "arabic_text" : "بوي فريند",
            "template_id" : [
                1
            ]
        }
    ]
}

Now I want the options to specific template id Let's say I want the options for template id 3 my result set should look like below.

    {
    "_id" : ObjectId("5912f2d9d5dcab2bf5ed4572"),
    "name" : "design",
    "hybris_name" : "design",
    "type" : "predefin_values",
    "options" : [
        {
            "englis_text" : "Straight",
            "arabic_text" : "بوي فريند",
            "template_id" : [
                1,
                3
            ]
        }
    ]
}
{
    "_id" : ObjectId("59130618d5dcab2bf5ed4573"),
    "name" : "fit",
    "hybris_name" : "",
    "fit" : "predefin_values",
    "options" : [
        {
            "englis_text" : "Slim",
            "arabic_text" : "واسع",
            "template_id" : [
                1,
                3
            ]
        }
    ]
}

How to write the mongo query for this?

Upvotes: 1

Views: 103

Answers (2)

Rubin Porwal
Rubin Porwal

Reputation: 3845

According to above mentioned description $elemMatch operator can be utilized to query for values contained in an embedded document.

Please try executing following query in MongoDB shell.

    db.collection.find({
    options: {
        $elemMatch: {
            template_id: {
                $all: [3]
            }
        }
    }
})

Upvotes: 0

Ra Ka
Ra Ka

Reputation: 3055

You need to use aggregate, first unwind and then match for 3.

db.collectionname.aggregate([{"$unwind":"$options"},{"$match":{"options.template_id":3}}])

Result:

{
  "_id": ObjectId("5912f2d9d5dcab2bf5ed4572"),
  "name": "design",
  "hybris_name": "design",
  "type": "predefin_values",
  "options": {
    "englis_text": "Straight",
    "arabic_text": "بوي فريند",
    "template_id": [
      1,
      3
    ]
  }
}
{
  "_id": ObjectId("59130618d5dcab2bf5ed4573"),
  "name": "fit",
  "hybris_name": "",
  "fit": "predefin_values",
  "options": {
    "englis_text": "Slim",
    "arabic_text": "واسع",
    "template_id": [
      1,
      3
    ]
  }
}

Upvotes: 2

Related Questions