Add $position option which takes a positive number.
db.col.save({_id:1, a: [3]})
db.col.update({}, {$push: {a: {$each:[1,2], $position:0 }}}) // pushes to front of array
If $position is equal to or greater than the array size then it will be ignored and is effectively a push to the end, which is the default behavior if not specified.
This will allow you to effectively $pop from the top and add to the bottom or vice versa using $slice since you push in either direction.
- depends on
-
SERVER-2362 Add new Deque/Set operation modifiers
- Closed
-
SERVER-6399 Refactor update() code
- Closed
- is duplicated by
-
SERVER-1824 Support for inserting into specific array index
- Closed
-
SERVER-2191 $push() to front of array
- Closed
-
SERVER-6873 Inserting a unique value into a specific position in an array
- Closed
- is related to
-
SERVER-13521 Allow fast $push to front of an array
- Backlog
-
SERVER-39446 $push/$pushAll to honor $position past end of array
- Closed
- related to
-
SERVER-991 $push with $slice + $sort
- Closed
-
SERVER-2036 $insert and $remove for arrays (currently $set, $unset, $push, and $pull are insufficient)
- Closed