
As shown, we can use addToSet, which won't add a duplicated value to an array:The third parameter of update is upsert(false by default).


Unfortunately Scott already has got movies in his loves field. This post is a mini-tutorial on how to deal with array fields in MongoDB, specifically arrays of items that may or may not be duplicated.Let's create a collection of unicorns, smilarly to examples: Find should now return something like this, the will vary:Adding a value into an arrayWe've found out that our unicorn Scott also loves playing games, so lets a new value into our document's loves array:Using push, we could insert into our the same value multiple times.Pushing a value if its uniqueWe have found out that all our unicorns love movies and want to update the whole collection.

Please let me know if this post has helped you or if you find anything to improve.
