Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Mongoose / MongoDB $addToSet functionality on array of objects

say I have this array property ('articles') on a Mongoose schema:

articles: [    {   kind: 'bear',   hashtag: 'foo'      },   {   kind: 'llama',   hashtag: 'baz',   },   {   kind: 'sheep',   hashtag: 'bar',   }  ] 

how can I use

$addToSet https://docs.mongodb.org/manual/reference/operator/update/addToSet/

to add to this array by checking the value of hashtag to see if it's unique?

For example, if I want to add the following object to the above array, I want Mongo to 'reject' it as a duplicate:

{   kind: 'tortoise',   hashtag: 'foo'  } 

because hashtag=foo has already been taken.

The problem is that I only know how to use $addToSet with simple arrays of integers...

for example, if articles looked like this:

articles: [ 1 , 5 , 4, 2] 

I would use $addToSet like this:

var data = {      "$addToSet": {      "articles": 9    }  }  model.update(data); 

but how can I accomplish the same thing with an array of objects where the unique field is a string, in this case 'hashtag'? The docs don't make this clear and it seems like I have searched everywhere..

thanks

like image 826
Alexander Mills Avatar asked Nov 06 '15 21:11

Alexander Mills


People also ask

How do I Upsert an array in MongoDB?

Learn how to update array fields in documents in MongoDB collections. You can use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.

How do I add elements to an array in MongoDB?

If the value is an array, $push appends the whole array as a single element. To add each element of the value separately, use the $each modifier with $push . For an example, see Append a Value to Arrays in Multiple Documents. For a list of modifiers available for $push , see Modifiers.

How do I remove an element from an array in MongoDB?

To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

How do you use $addToSet?

The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array. The $addToSet operator has the form: { $addToSet: { <field1>: <value1>, ... } } To specify a <field> in an embedded document or in an array, use dot notation.


1 Answers

You need to use the $ne operator.

var data = { 'kind': 'tortoise', 'hashtag': 'foo' }; Model.update(     { 'articles.hashtag': { '$ne': 'foo' } },      { '$addToSet': { 'articles': data } } ) 

This will update the document only if there is no sub document in the "article" array with the value of hashtag equals to "foo".

As @BlakesSeven mentioned in the comment

The $addToSet becomes irrelevant once you are testing for the presence of one of the values, so this may as well be a $push for code clarity. But the principle is correct since $addToSet works on the whole object and not just part of it.

Model.update({      { 'articles.hashtag': { '$ne': 'foo' } },      { '$push': {'articles':  data } } ) 
like image 177
styvane Avatar answered Oct 20 '22 07:10

styvane