MongoDb: Difference between $push/$addtoset

Mongodb

Mongodb Problem Overview


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.

Mongodb Solutions


Solution 1 - Mongodb

$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: {items:2}}); // this will update the document. new document {_id: "docId", items:[1,2,2]}

Solution 2 - Mongodb

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

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

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.

Solution 3 - Mongodb

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

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

  • $push operations (that actually modified the array) are replicated as $set.items.INDEX: item
  • $addToSet operations (that actually modified the array) are replicated as $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.

Solution 4 - Mongodb

$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.

Solution 5 - Mongodb

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

Solution 6 - Mongodb

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

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;

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmilio LópezView Question on Stackoverflow
Solution 1 - MongodbcubbukView Answer on Stackoverflow
Solution 2 - MongodbOleksandr LukichovView Answer on Stackoverflow
Solution 3 - MongodbAllan LeiView Answer on Stackoverflow
Solution 4 - MongodbRemarioView Answer on Stackoverflow
Solution 5 - MongodbVipul PandeyView Answer on Stackoverflow
Solution 6 - MongodbHasan RezaView Answer on Stackoverflow