mongodb - Push element in any position of array in the subdocument -
in mongodb 2.6 can use $position
(http://docs.mongodb.org/master/reference/operator/update/position/) modifier specifies location in array during update of array in document. insert in array in subdocument.
document schema:
{ subdoc: { array: ['0', '1', '2', '5', '6'] } }
the following update pushes elements in end of array
..
db.collection.update( { _id: tsid }, {$push: { 'subdoc.array': { $each:['3', '4'], $position:3 } }});
so, result
{ subdoc: { array: ['0', '1', '2', '5', '6', '3', '4'] } }
but expect
{ subdoc: { array: ['0', '1', '2', '3', '4', '5', '6'] } }
is possible in mongodb 2.6?
it's fair proposition in question, have concept wrong.
the first of have missed concept arrays in general have entries starting @ index of 0
first element, "positioning" out one unit in case , should have been:
db.collection.update( { _id: tsid }, {$push: { 'subdoc.array': { "$each":["3", "4"], "$position": 3 } }} )
and since inserting @ correct position, elements in correct place.
Comments
Post a Comment