[SERVER-13572] Push element in any position of array in the subdocument Created: 14/Apr/14  Updated: 03/Jun/14  Resolved: 14/Apr/14

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

Type: Bug Priority: Major - P3
Reporter: Roman Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-7101 Commands should error on missing/inva... Open
Operating System: ALL
Participants:

 Description   

In MongoDB 2.6 we can use $position modifier to specifies the location in the array during update of an array in a document. But I would like to insert in an array in a subdocument.

// Document schema:
{  subdoc: {
    array: ['0', '1', '2', '5', '6'] } }
 
// The following update pushes the elements in the end of array..
db.collection.update(
   { _id: tsId },
   {$push: { 'subdoc.array': { $each:['3', '4'], $position:2 } }});
 
// So, the result is
{  subdoc: {
    array: ['0', '1', '2', '5', '6', '3', '4'] }}
 
// But I expect
{  subdoc: {
    array: ['0', '1', '2', '3', '4', '5', '6'] }}



 Comments   
Comment by Roman [ 14/Apr/14 ]

sorry for inattention, the mongodb version is 2.4.10! Please close this issue..

Comment by Scott Hernandez (Inactive) [ 14/Apr/14 ]

Are you sure you are using 2.6.0? Please run "db.version()" to verify that the server is 2.6.0.

I cannot reproduce this with 2.6.0 locally:

> db.version()
2.6.0
> db.a.save({ subdoc: { array: ['0', '1', '2', '5', '6'] } ,_id:1})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })
> db.a.find()
{ "_id" : 1, "subdoc" : { "array" : [ "0", "1", "2", "5", "6" ] } }
> db.a.update({}, {$push:{"subdoc.array": {$each:['3','4'], $position:2}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.a.find()
{ "_id" : 1, "subdoc" : { "array" : [ "0", "1", "3", "4", "2", "5", "6" ] } }

Generated at Thu Feb 08 03:32:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.