Search code examples
mongodb

$set only when the field does not exists when using the array operator in Mongodb


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?


Solution

  • You can use arrayFilters,

    • create a variable b for bar is exists
    • create a variable t for test is not exists
    db.myCollection.updateMany(
        {},
        { $set: { 'foo.$[b].bar.$[t].test': 42 } },
        {
            arrayFilters: [
                { "b.bar": { $exists: true } },
                { "t.test": { $exists: false } }
            ]
        }
    )