title: Using Mongoose / MongoDB $addToSet functionality on array of objects
body: <p>say I have this array property ('articles') on a Mongoose schema:</p>
  
  <pre><code>articles: [ 
  
   {
    kind: 'bear',
    hashtag: 'foo'    
   },
  
   {
    kind: 'llama',
    hashtag: 'baz',
    },
  
   {
    kind: 'sheep',
    hashtag: 'bar',
    }
  
  ]
  </code></pre>
  
  <p>how can I use</p>
  
  <blockquote>
    <p>$addToSet <a href="https://docs.mongodb.org/manual/reference/operator/update/addToSet/" rel="noreferrer">https://docs.mongodb.org/manual/reference/operator/update/addToSet/</a></p>
  </blockquote>
  
  <p>to add to this array by checking the value of hashtag to see if it's unique?</p>
  
  <p>For example, if I want to add the following object to the above array, I want Mongo to 'reject' it as a duplicate:</p>
  
  <pre><code>{
    kind: 'tortoise',
    hashtag: 'foo'
  
  }
  </code></pre>
  
  <p>because hashtag=foo has already been taken. </p>
  
  <p>The problem is that I only know how to use <code>$addToSet</code> with simple arrays of integers...</p>
  
  <p>for example, if articles looked like this:</p>
  
  <pre><code>articles: [ 1 , 5 , 4, 2]
  </code></pre>
  
  <p>I would use $addToSet like this:</p>
  
  <pre><code>var data = {
  
      "$addToSet": {
       "articles": 9
     }
  
  }
  
  model.update(data);
  </code></pre>
  
  <p>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..</p>
  
  <p>thanks</p>