Wykorzystanie ujemny wskaźnik z $ ustawiony prepend, testowane w Mongo v2.2:
> db.test.insert({'array': [4, 5, 6]})
> db.test.find()
{ "_id" : ObjectId("513ad0f8afdfe1e6736e49eb"),
"array" : [ 4, 5, 6 ] }
//prepend 3
> db.test.update({"_id" : ObjectId("513ad0f8afdfe1e6736e49eb")},
{'$set': {'array.-1': 3}})
> db.test.find()
{ "_id" : ObjectId("513ad0f8afdfe1e6736e49eb"),
"array" : [ 3, 4, 5, 6 ] }
//prepend 2
> db.test.update({"_id" : ObjectId("513ad0f8afdfe1e6736e49eb")},
{'$set': {'array.-1': 2}})
> db.test.find()
{ "_id" : ObjectId("513ad0f8afdfe1e6736e49eb"),
"array" : [ 2, 3, 4, 5, 6 ] }
//prepend 1
> db.test.update({"_id" : ObjectId("513ad0f8afdfe1e6736e49eb")},
{'$set': {'array.-1': 1}})
> db.test.find()
{ "_id" : ObjectId("513ad0f8afdfe1e6736e49eb"),
"array" : [ 1, 2, 3, 4, 5, 6 ] }
Zobacz http://stackoverflow.com/questions/ 10094573/use-mongodb-array-as-stack # comment12929388_10094573 –
możliwy duplikat [jak dodać wartość do początku tablicy w mongodb?] (Http://stackoverflow.com/questions/7936019/how-do -i-add-a-value-to-the-top-of-array-in-mongodb) –