0

Record I have:

[   {
    "url": "a",
    "nested_object_keys": [
      {
        "vendor": "terraform",
        "tag": "some_tag_1"
      },
      {
        "vendor": "terraform",
        "tag": "some_tag_2"
      },
       {
        "vendor": "terraform",
        "tag": "some_tag_3"
      },
      {
        "vendor": "terraform",
        "tag": "some_tag_4"
      }

    ]   }

Expected Output: I should only get the array objects that matches with the value of key : some_tag_1 (if only key with this value present) or some_tag_2 ((if only key with this value present)or both if both values present.

[   {
        "url": "a",
        "nested_object_keys": [
          {
            "vendor": "terraform",
            "tag": "some_tag_1"
          },
          {
            "vendor": "terraform",
            "tag": "some_tag_2"
          }

        ]   }

I have tried elemMatch with so many operators in, and, or , all. But I got result like all the available objects in array or only single document that matched first but not the both. Limitation: Aggregation should not be used.

Rambo
  • 49
  • 1
  • 9
  • 1
    Not possible to do with `.find()` as the projection will only return the first matching element in the array field. Possible duplicate: [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – Yong Shun Mar 22 '23 at 07:05
  • @YongShun, Thanks for the response . Therefore should use only Aggragation for the requirement. – Rambo Mar 24 '23 at 05:05

0 Answers0