A
A
Alexander Wolf2016-06-16 15:47:42
MongoDB
Alexander Wolf, 2016-06-16 15:47:42

How to move an element in an array (mongodb)?

Good afternoon! I have a document:

{ arr: [{ _id: 'a', val: 1 }, { _id: 'b', val: 4 }, { _id: 'd', val: 0 }, { _id: 'c', val: 8 }] }

And I need to move the array element "C" to position 2 (that is, before "D").
You can request a document on the server, get the element I need, pull it out of its position and stick it in the right one. But this is not a true way. Is there any way to do this in one request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qazqazasda, 2016-06-16
@mannaro

$pull and $push
$pull - obviously.
and $push is something like this:
$push: { "items" : { $each : [ "item" ], $position : 1 } }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question