[SERVER-39446] $push/$pushAll to honor $position past end of array Created: 08/Feb/19  Updated: 06/Dec/22  Resolved: 29/Jun/19

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Justin LaBreck Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-13478 Resolve ambiguity of positional array... Backlog
related to SERVER-2363 $push/$pushAll support for insertion ... Closed
Assigned Teams:
Query
Participants:

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



 Comments   
Comment by Asya Kamsky [ 29/Jun/19 ]

In 4.2 this can now be achieved using update pipelines with aggregation expressions.

In that case this result could be achieved directly by using $concatArrays...

Generated at Thu Feb 08 04:52:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.