i have the following document
{
"_id" : ObjectId("605a62eadefa2a09797f1ae3"),
"address" : [
{
"_id" : ObjectId("605a62eadefa2a09797f1ae4"),
"street" : "King AS",
"state" : "CA",
"zip" : 12111
},
{
"_id" : ObjectId("605a62f9defa2a09797f1ae5"),
"street" : "123 Leon Street",
"state" : "CA",
"zip" : 12121
}
],
"firstName" : "James",
"__v" : 1
}
i wanted to remove the nested address document completely but somehow leaves me with that id value, instead of being completely empty.
i did something like this:
findOneAndUpdate({ "_id": ObjectId("605a62eadefa2a09797f1ae3"), }, { address: { $elemMatch: {"_id": ObjectId("605a62f9defa2a09797f1ae5")} } }, {$unset:{address: ""}})
but somehow it tends to delete both the nested address object elements. and I’m left with something like this:
address: [ { _id: 605a697be0f5e50a3561dc44 } ]
what i’d ideally want is something like this:
i’d want to delete specific address only and also, remove that subdocument from the nested array subdocuments. Something like this:
lets say i want to delete the second nested documents, then the final documents should look like this:
{
"_id" : ObjectId("605a62eadefa2a09797f1ae3"),
"address" : [
{
"_id" : ObjectId("605a62eadefa2a09797f1ae4"),
"street" : "King AS",
"state" : "CA",
"zip" : 12111
}
],
"firstName" : "James",
"__v" : 1
}
thank you kindly.