0

Here is an example : example

I am trying to add 'X' and 'Y' only to the subcategory arrays that contains EDUCATION key. What is happening here is that it adds 'X' and 'Y' to all subcategory if at least on of them contains the EDUCATION key.

1
  • your playground link is missing Commented Jun 17, 2022 at 10:11

1 Answer 1

1

You need to use arrayFilters, to match specific element in an array.

db.collection.update({},
{
  "$push": {
    "data.category.$[elem].subcategory": {
      "$each": [
        "X",
        "Y"
      ]
    }
  }
},
{
  multi: true,
  arrayFilters: [
    {
      "elem.subcategory": "EDUCATION"
    }
  ]
})

Check this link

Sign up to request clarification or add additional context in comments.

4 Comments

Doesnt work sir
It only adds to the docs, whose subcategory contains EDUCATION isn't it expected? Can you paste the expected output in the question
this only adds to the first subcategory list. if you add $[] it will fail again
Thnx sir ;) ---

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.