-
Type: Improvement
-
Resolution: Won't Do
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Write Ops
-
Query
Add support for multiple conditional updates in a single update operation. An array of match + update will be executed against each found document. All positional matching is done on the update match, not the query. See below for examples.
db.coll.update( query, {$updates: [ {when:{a:null}, do:{$set:{a:3}}}, // if missing or null set it {when:{_id:null}, do:{ $set:/* initial values */}}, // same as $setOnInsert since _id will be null/missing {when:{votes:{$lt:19}}, do:{$inc:{votes:1}}},// max for field of 20 {when:{votes:{$gt:20}}, do:{$set:{votes:20}}},// max for field of 20, cleanup bad values {when:{tags:{$ne:null}}}, do:{$push:{tags:"a"}}},// $push if tags array exists {when:{tags:null}, do:{$set:{tags:["a"]}}},// replace null with tags array if incorrectly set to null {when:{$not: {"users.id":"scott"}}, do:{$push:{users:{id:"scott", views:0 }}}},// Support for "push if missing" (kinda like upsert for arrays) {when:{"users.id":"scott"}, do:{$inc:{"users.$.views:1 }}}, //SERVER-1014 example $pull first match of 2 in array {when:{vals:2}, do:{$unset:{"vals.$":true}}},// unset array element (sets to null) {when:{vals:null}, do:{$pull:{vals:null}},// remove nulls from array //SERVER-1050 example (no match/do needed -- unconditional) {$push:{vals:-200}}, {$pop:{vals:-1}}}, //SERVER-1947 $set w/positional and $push to same array {$set: { "version" : 2, "name" : "NEWNAME"}, $inc: {"versionsCount" : 1 }, $push: {"versions" : { "z" : "anewone" }}} {when:{ "versions.version" : 1}, do:{$inc:{"versions.$.versionToInstant" : 1000}}} ]})
The server would execute the query, run through each conditional update element in the array (in order) and apply the update only if the match succeeds for the document.
- is duplicated by
-
SERVER-34973 Upsert querying an array, then pushing to this array with simple values
- Closed
-
SERVER-30587 Doing Upsert in Subdocuments when performing 'bulk' operations
- Closed
-
SERVER-40266 Add on modified option to $currentDate
- Closed
- is related to
-
SERVER-20203 upsert querying an array field and $push-ing to that same field yields confusing error
- Closed
-
SERVER-3326 Support $ positional operator with an upsert
- Closed
-
SERVER-13578 add $setOnUpdate to update operation
- Closed
-
SERVER-14094 Conditional Updates: Conditionally update secondary fields if primary fields updated
- Closed
-
SERVER-22102 New update operator for compare-and-set pattern
- Closed
-
SERVER-28789 Add a 'initial value' parameter to $inc
- Closed
-
SERVER-30976 Conditional Update/Insert Operation, Batched to a Find/Find-and-Modify Op
- Closed
-
SERVER-33482 update existing item or push a new item to an array in a document
- Closed
-
SERVER-1014 A modifier to delete a single value from an array
- Closed
-
SERVER-7894 A $setOnChange command would be very helpful
- Closed
-
SERVER-10523 Lower/Upper limit for $inc
- Closed
-
SERVER-2643 Allow Field Name Duplication with Modifiers
- Closed
-
SERVER-30587 Doing Upsert in Subdocuments when performing 'bulk' operations
- Closed
-
SERVER-786 Update a size field with addToSet
- Closed
-
SERVER-1050 not allowed to $push and $pop to same field in same update
- Closed
-
SERVER-1947 can't push and set in one operation
- Closed
- related to
-
SERVER-2476 New $update atomic modifier for changing array elements
- Closed
-
SERVER-6399 Refactor update() code
- Closed
-
SERVER-8777 update the data based on old values and new values with the same Unique Key
- Closed