你可以有蒙戈$推prePEND,而不是追加? [英] Can you have mongo $push prepend instead of append?
问题描述
我想有新增推在我集的开头,而不是追加到结束的时候我做了蒙戈$推。
I'd like to have push add at the beginning of my set rather than appended to the end when I do a mongo $push.
是否有可能做一个原子推送更新,增加了元素作为第一个而不是最后?
Is it possible to do an atomic push update that adds elements as the first rather than the last?
2014年更新:是的,你可以一>
推荐答案
由于MongoDB的v2.5.3,有一个新的 $位置
操作符,你可以沿着包括在 $每个
运营商为你的 $努力的一部分
查询指定数组中的位置上,你想插入值。
As of MongoDB v2.5.3, there is a new $position
operator that you can include along with the $each
operator as part of your $push
query to specify the location in the array at which you would like to insert a value.
下面是从文档页面为例,添加元素20和30的2数组索引:
Here's an example from the docs page to add the elements 20 and 30 at the array index of 2::
db.students.update( { _id: 1 },
{ $push: { scores: {
$each: [ 20, 30 ],
$position: 2
}
}
}
)
参考:<一href=\"http://docs.mongodb.org/master/reference/operator/update/position/#up._S_position\">http://docs.mongodb.org/master/reference/operator/update/position/#up._S_position
这篇关于你可以有蒙戈$推prePEND,而不是追加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!