I have one entry in my Data
collection and is as follows:
{
"data": [
{
"key1": {
"name": "test"
}
},
{
"key1": {
"name": "test2"
}
}
]
}
I would like to remove the object where key1.name: 'test2'
from the data
array.
I have tried the following:
db.Data.updateOne({}, { $pull: { 'data': { 'key1.name': 'test2' } } })
This works fine in a container instance of mongodb (latest).
But in a DocumentDB with engine version 5.0, it does not work and returns MongoServerError: Dotted field name is not valid for storage
.
Is there any way for me to use the $pull operator in a DocumentDB where I’m targeting an object via a nested field?
2
Answers
This doesn’t exactly answer how to use
$pull
for nested objects, but in DocumentDB you can use below workaround where you can try to set all such objects tonull
usingupdate
commandarray positional operator
. You can write additional query to even clean thisdata
array if required.DocumentDB is an emulation of MongoDB and may not support all the features in MongoDB.
One of the workarounds at the moment could be using
$filter
to filter thedata
array and update back to the collection.Mongo Playground