let's say I have this array property ('article') in the 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/
add to this array by checking the hashtag value to see if it is 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 the hashtag = foo is already taken.
The problem is that I know how to use $addToSet with simple arrays of integers ...
for example, if the articles look 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 do the same with an array of objects where the unique field is a string, in this case 'hashtag'? The docs don't make it clear, and it seems like I searched everywhere ...
thanks