-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Labels:
Description
Page: https://docs.mongodb.com/manual/reference/command/update/index.html
Consider the following sequence of commands which results in a document validation failure:
> db.coll.insertMany([{a: 1, b:1}, {a: 2, b:2}, {a: 3, b:3}, {a:1, b:4}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("60429200cb07b1d94070d658"), ObjectId("60429200cb07b1d94070d659"), ObjectId("60429200cb07b1d94070d65a") ] } > db.coll.createIndex({b: 1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.runCommand({collMod: "coll", validator: {a: {$ne: 200}}}) { "ok" : 1 } > db.coll.updateMany({}, {$mul: {a: 100}}, {hint: {b: 1}}) > db.coll.find() { "_id": ObjectId("6047934b5921dc2713d2b1e8"), "a": 100, "b": 1 } { "_id": ObjectId("6047934b5921dc2713d2b1e9"), "a": 2, "b": 2 } { "_id": ObjectId("6047934b5921dc2713d2b1ea"), "a": 3, "b": 3 } { "_id": ObjectId("6047934b5921dc2713d2b1eb"), "a": 1, "b": 4 }
Note that once a single document fails to update, no other documents are updated. While this is correct behavior, some users may find this surprising as they might expect that the command will continue updating documents after a failure. As such, we should update the documentation of the ‘update’ command to describe what happens in the case of a multi-update that fails.