-
Type: Improvement
-
Resolution: Won't Do
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: Write Ops
-
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).
- related to
-
SERVER-13478 Resolve ambiguity of positional array updates when the path does not exist
- Backlog
-
SERVER-2363 $push/$pushAll support for insertion at any position (top/bottom/ordinal)
- Closed