-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Write Ops
-
Query Optimization
The upsert behaviour in update does not handle arrays the same way for insert and update.
Upsert is only useful in the case that you don't know if the object already exists.
I need a way to call upsert with arrays that creates the same document shape for both insert and update. This will allow efficient manipulation of array contents.
The example below is a simple demonstration of the problem, using the mongo shell.
Simplest version, mongo shell code. The first upsert adds to the 'arr' array, the second creates an 'arr' document, the third creates an 'arr' array.
db.onearray.insert({_id:"oa1",arr: [1]}) db.onearray.update({_id:"oa1"},{$set: {"arr.1":2}},true) db.onearray.update({_id:"oa2"},{$set: {"arr.1":3}},true) db.onearray.update({_id:"oa3"},{$set: {"arr":[4]}},true) db.onearray.find() { "_id" : "oa1", "arr" : [ 1, 2 ] } { "_id" : "oa2", "arr" : { "1" : 3 } } { "_id" : "oa3", "arr" : [ 4 ] }
Slightly more complex version.
The first upsert modifies the 'bottom' array. The second upsert creates a 'bottom' document with key "0". The third upsert creates a 'bottom' array.
db.twoarrays.insert({_id:"ta1",rnumber: 0,top:[{tnumber:1,bottom:[{a:1,b:2}]}]}) db.twoarrays.update({_id:"ta1"},{$set:{"top.0.bottom.0": {a:2,b:2}}},true) db.twoarrays.update({_id:"ta1"},{$set:{"top.1.bottom.0": {a:3,b:3}}},true) db.twoarrays.update({_id:"ta1"},{$set:{"top.2.bottom": [{a:4,b:4}]}},true) db.twoarrays.find()
- is duplicated by
-
SERVER-46695 Allow arrays creation during new record upsert when specifying dotted sub-paths inside $set:{} command
- Closed
- is related to
-
SERVER-39446 $push/$pushAll to honor $position past end of array
- Closed
- related to
-
SERVER-4753 array indexes and object subfields should be referenced with different syntax
- Backlog