How to updateMany documents and return a list with the updated documents?

Hi everyone,

I have a list of orders that I want to update but some objects have null values on some parameters.

If I want to update “parameter.subparameter”, maybe is null in the current object that is being updated so the updateMany() will fail in this case.

var orders = [
    "ORDER_N_1",
    "ORDER_N_2",
    "ORDER_N_3"
]
try {
    var response = db.ORDER_COLLECTION.updateMany(
        {
            Order_Number: { $in: orders },
        },
        {
            $set: {
                "parameter.subparameter":"1"
            },
        }
    );
} catch(error) {
    print(error)
}
print("response: "+JSON.stringify(response, null, 2))

And the response is:

response: {
    "acknowledged": true,
    "matchedCount": 2,
    "modifiedCount": 2
}

This is the response object I get from the updateMany() method. As we can see, we have 3 objects to be updated but just only 2 were. All 3 objects match but as we encounter an error, the execution stopped so I want to know which document failed.

Is there an option to make updateMany() specify which order failed while being updated?

Hi @Andres_Mendez ,

I am not aware of a configuration to do that with just what you described.

However, you can use a findAndUpdate command with returning a new document flag , and updating a last modified time field. Sorting by this time together with the known amount of updated documents should give you the ids.

Thanks
Pavel