MongoDb:$push/$addtoset 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27248556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:13:32  来源:igfitidea点击:

MongoDb: Difference between $push/$addtoset

mongodb

提问by Emilio López

I read the documentation in the MongoDb and I used a simple proves and I only look that: Push is sorting the array but addtoSetisn't it.

我阅读了 MongoDb 中的文档,并使用了一个简单的证明,我只看:推送正在对数组进行排序,但addtoSet不是。

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.

另一个认为,如果它可以是西班牙语或简单的英语,我会很感激。

回答by cubbuk

$addToSetdo not add the item to the given field if it already contains it, on the other hand $pushwill add the given object to field whether it exists or not.

$addToSet如果项目已经包含它,则不要将它添加到给定的字段中,另一方面$push,无论是否存在,都会将给定的对象添加到字段中。

{_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]}

回答by Oleksandr Lukichov

$push- adds items in the order in which they were received. Also you can add same items several times.

$push- 按照收到的顺序添加项目。您也可以多次添加相同的项目。

$addToSet- adds just unique items, but order of items is not guaranteed.

$addToSet- 只添加唯一的项目,但不保证项目的顺序。

If you need to add unique items in order, you can group and add elements via $addToSet, then $unwind the array with elements, $sort by items, and then do $group again with $push items.

如果您需要按顺序添加唯一项,您可以通过 $addToSet 对元素进行分组和添加,然后用元素 $unwind 数组,按项进行 $sort,然后再用 $push 项执行 $group。

回答by Allan Lei

Along side the differences that others have mentioned

除了其他人提到的差异

  • $push: Appends an object to an array
  • $addToSet: Adds an object to an array if it does not exists
  • $push: 将对象追加到数组
  • $addToSet: 如果对象不存在,则将其添加到数组中

There is a difference in replication. (This can be seen if by taking a look at local.oplog.rs)

复制是有区别的。(这可以通过查看来了解local.oplog.rs

  • $pushoperations (that actually modified the array) are replicated as $set.items.INDEX: item
  • $addToSetoperations (that actually modified the array) are replicated as $set.items: [THE_ENTIRE_ARRAY]
  • $push操作(实际上修改了数组)被复制为 $set.items.INDEX: item
  • $addToSet操作(实际上修改了数组)被复制为 $set.items: [THE_ENTIRE_ARRAY]

If you are dealing with large arrays, then the difference might be significant

如果您正在处理大型数组,那么差异可能很大

So while something like (the typical use case to maintain unique array)

因此,虽然类似(维护唯一数组的典型用例)

db.items.updateOne(
  {_id: 'my-id', 'items': {'$ne': 'items1'},
  {'$push': {
    'items': 'item1',
  }}
)

db.items.updateOne(
  {_id: 'my-id'},
  {'$addToSet': {
    'items': 'item1',
  }}
)

might end up with the same resulting document, there is a difference in the replicated operation.

可能最终得到相同的结果文档,复制操作存在差异。

回答by Vipul

As the name suggest $addToSet (set) wont allow duplicates while $push simply add the element to array

顾名思义 $addToSet (set) 不允许重复,而 $push 只是将元素添加到数组

回答by Remario

$addToSet and $push does the same thing, however $push just pushes any item disregarding the duplication causing redundancy. The former pushes only unique items, no duplication.

$addToSet 和 $push 做同样的事情,但是 $push 只是推送任何项目,而忽略导致冗余的重复。前者只推送唯一的项目,没有重复。

回答by Hasan

$push: Inserts the value to an array in the resulting document. eg;

$push:将值插入到结果文档中的数组中。例如;

db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])

$addToSet: Inserts the value to an array in the resulting document but does not create duplicates. eg;

$addToSet:将值插入到结果文档中的数组中,但不创建重复项。例如;

db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])