Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-13478

Resolve ambiguity of positional array updates when the path does not exist

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major - P3 Major - P3
    • None
    • None
    • Write Ops
    • Query Optimization

    Description

      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() 
      

      Attachments

        Activity

          People

            backlog-query-optimization Backlog - Query Optimization
            GerryF Gerry F
            Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated: