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

$push/$pushAll to honor $position past end of array

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Do
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Write Ops
    • Labels:
      None
    • Query

      The $position operator ($push/$pushAll) has a restriction that ignores all positions past the end of an array:

      If the value of <num> is greater or equal to the length of the array, the $position modifier has no effect and $push adds elements to the end of the array.

      https://docs.mongodb.com/manual/reference/operator/update/position/#position

      This seems intentional in the original design (SERVER-2363) but limits common use cases. It also differs from behavior elsewhere using dot notation on an existing array:

      > db.foo.insertOne({ _id: "d", field: [] });
      { "acknowledged" : true, "insertedId" : "d" }
      > db.foo.updateOne({ _id: "d" }, { $set: { "field.4": 5 } }, { upsert: 1 })
      { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
      > db.foo.find(}
      { "_id" : "d", "field" : [ null, null, null, null, 5 ] }
      

      It would seem logical, beneficial, and consistent to enable the positional operator to add the values of $each to any position of the array. This provides additional value for upserts by providing an alternative to dot notation that may create inconsistent documents (SERVER-13478).

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            justin.labreck@mongodb.com Justin LaBreck
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: