MongoDb:$ push/$ addtoset之间的区别 [英] MongoDb: Difference between $push/$addtoset
本文介绍了MongoDb:$ push/$ addtoset之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我阅读了MongoDb中的文档,并使用了简单的证明,但我只发现:
Push正在对数组进行排序,但addtoSet
不是.
I read the documentation in the MongoDb and I used a simple proves and I only look that:
Push is sorting the array but addtoSet
isn't it.
对我来说,视觉上是一样的,我不知道区别.
For me visually is the same, I don't know the difference.
有人可以解释一下我的区别吗?
Could anybody explain me the difference?
另一个人认为,如果它可以是西班牙语或简单的英语,我会予以保留.
Another think if it could be in spanish or in a simple english, i'll aprecite it.
推荐答案
$addToSet
如果该项目已包含该项目,则不要将其添加到给定的字段中,另一方面,$push
会将给定的对象添加到该字段中它存在与否.
$addToSet
do not add the item to the given field if it already contains it, on the other hand $push
will add the given object to field whether it exists or not.
{_id: "docId", items: [1, 2]}
db.items.update({_id:"docId"}, {$addToSet:{items: 2}}); // This won't update the document as it already contains 2
db.items.update({_id:"docId"}, {$push: {item:2}}); // this will update the document. new document {_id: "docId", items:[1,2,2]}
这篇关于MongoDb:$ push/$ addtoset之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文