1

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?

2 Answers 2

2

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
    ]
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I tried with same but $elemMatch will return all the options of a document because it's doesn't filter from all the options. In my requirement, I want specific options value of an attribute with given template_id. your solution doesn't work for me. anyway, thanks for the replay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.