I need to set a default value to my documents but only when the field does not exists. My problem is that I am using an array operator.
My request is like this :
db.myCollection.updateMany({}, { $set: { 'foo.$[].bar.$[].test': 42 }})
myCollection
contains the followings two documents:
{
foo: [
{ bar: [{ a: 1 }, { a: 2, test: 18 }] },
{ bar: [{ a: 1, test: 20 }, { a: 2, test: 18 }] }
]
}
{
foo: [
{ bar: [{ a: 1, test: 40 }, { a: 2, test: 18 }] },
{ bar: [{ a: 1, test: 20 }, { a: 2, test: 18 }] }
]
}
But if the property test
exists, I don't want to overwrite it. So in my example, I only want to set the default value of one subelement (in the first document).
How can i change my request to do it?
You can use arrayFilters,
b
for bar
is existst
for test
is not existsdb.myCollection.updateMany(
{},
{ $set: { 'foo.$[b].bar.$[t].test': 42 } },
{
arrayFilters: [
{ "b.bar": { $exists: true } },
{ "t.test": { $exists: false } }
]
}
)