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

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

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor - P4
    • Resolution: Won't Do
    • None
    • None
    • Write Ops
    • None
    • Query

    Description

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

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: