Pushing a value at a specific position in an array

69 views
Skip to first unread message

Jan Riechers

unread,
Aug 27, 2012, 5:02:37 AM8/27/12
to mongod...@googlegroups.com
Hello,

I try to push a unique value inside of an array, the array consist of
subarray-items, so it looks like this:

[ [item1, 0,0], [item2, 0,0]... ]

Now I wanted to use push to insert an item between item1 and item2, but
when using "$push" - the result looks like the following:

[ [item1, 0,0], [item2, 0,0, [item3, 0,0] ]... ]

So instead of pushing the value using the positional operator, the item
gets pushed inside of the 2nd item.

Is there any way, using the Python driver, to insert an item at a
specific position so the result looks like the following?

[ [item1, 0,0], [item3, 0,0 ], [item2, 0,0]... ]

I know that $push more works like append in Python, but basically I need
a array.insert(element, AtPositionX).

I would appreciate any hint, as I want to avoid a sort inside upon
retrieval of the data and the order of the array should to the trick by
itself.

Help is much appreciated!

Jan

Jan Riechers

unread,
Aug 27, 2012, 11:57:50 AM8/27/12
to mongod...@googlegroups.com
On 27.08.2012 17:47, lwb wrote:
> Hi Jan,
>
> First, to insert the array [item3, 0,0] into your larger array, you want
> to use the $pushAll rather than $push -- this will append the whole
> [item3, 0, 0] array into your array.
>
> Currently, there is no $insert operator that will allow you to insert an
> object into a specific place in your array. You can file a ticket
> requesting this feature at jira.mongodb.org . You can create an account
> on that page, and then can create a new issue using the button in the
> top right corner.
>

Hello Louisa,

I created a ticket in Jira at
https://jira.mongodb.org/browse/SERVER-6873 with an example included.

As for $pushAll - I don't think that's the desired behavior as $push.

Referring to
http://www.mongodb.org/display/DOCS/Updating#Updating-%24pushAll
"every" item of the Array would been pushed into the container array
instead of just appending the whole Array into the container (in my case
the array item) to the container array itself.

The difference would be:
$pushAll [ [Item1, 0,0], [Item2, 0,0], Item3, 0,0 ] <- and this would be
wrong for my case, the data should look like:
[ [Item1, 0,0], [Item2, 0,0], [Item3, 0,0] ] (notice the extra [] here)
- its encapsulated as an additional array inside the container array

But thank you still for answering, would be great if you could implement
this feature and roll it into the (python) and other drivers and the
core! :)

Regards
Jan

Reply all
Reply to author
Forward
0 new messages