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

{
        "_id" : ObjectId("605a3a82c8bbb404f4e6b643"),
        "address" : [
                {
                        "_id" : ObjectId("605a3a82c8bbb404f4e6b644"),
                        "street" : "123 Leon Street",
                        "state" : "WE",
                        "zip" : 11312
                },
                {
                        "_id" : ObjectId("605a3b3bc8bbb404f4e6b645"),
                        "street" : "123 King Street",
                        "state" : "WE",
                        "zip" : 99998
                }
        ],
        "firstName" : "Tetet",
        "__v" : 1
}

lets say i have the id for the second nested object.

 "_id" : ObjectId("605a3b3bc8bbb404f4e6b645")

how to get only the document that is related to that id and not the whole document?

so far I have tried:

db.contacts.find({"_id" : ObjectId("605a3a82c8bbb404f4e6b643")}, {"address._id": ObjectId("605a3b3bc8bbb404f4e6b645")}).pretty()

and gotten:

{
        "_id" : ObjectId("605a3a82c8bbb404f4e6b643"),
        "address" : [
                {
                        "_id" : ObjectId("605a3b3bc8bbb404f4e6b645")
                },
                {
                        "_id" : ObjectId("605a3b3bc8bbb404f4e6b645")
                }
        ]
}

I would ideally want to have the nested documents, not just the id. Can anyone help me?
Thank you kindly

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

is there a way to just get

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

there are many and more

db.collection.aggregate([
  {
    "$match": {
      "_id": ObjectId("605a3a82c8bbb404f4e6b643")
    }
  },
  {
    "$unwind": "$address"
  },
  {
    "$match": {
      "address._id": ObjectId("605a3b3bc8bbb404f4e6b645")
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$address"
    }
  }
])

this also works

db.collection.aggregate([
  {
    "$match": {
      "_id": ObjectId("605a3a82c8bbb404f4e6b643")
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {
        "$first": {
          "$filter": {
            "input": "$address",
            "as": "item",
            "cond": {
              $eq: [
                "$$item._id",
                ObjectId("605a3b3bc8bbb404f4e6b645")
              ]
            }
          }
        }
      }
    }
  }
])
2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.