How to only get the array nested subdocuments with that document id and not having to iterate through it

Hi,
with this query+projection

find({
  "_id": ObjectId("605a3a82c8bbb404f4e6b643")
},
{
  address: {
    "$elemMatch": {
      "_id": ObjectId("605a3b3bc8bbb404f4e6b645")
    }
  }
})

you get

[
  {
    "_id": ObjectId("605a3a82c8bbb404f4e6b643"),
    "address": [
      {
        "_id": ObjectId("605a3b3bc8bbb404f4e6b645"),
        "state": "WE",
        "street": "123 King Street",
        "zip": 99998
      }
    ]
  }
]

is it what you want?

Sincerely,

3 Likes