title: mongodb $addToSet to a non-array field when update on upsert
body: <p>My recent project encountered the same problem as this one: <a href="https://stackoverflow.com/questions/22309869/mongodb-cannot-upsert-if-query-object-and-update-object-contain-a-same-property" title="MongoDB: cannot upsert if query object and update object contain a same property , &#39;Cannot apply $addToSet modifier to non-array&#39;">the question</a></p>
  
  <pre><code>db.test.update(
      {name:"abc123", "config.a":1  }, 
      {$addToSet:{ config:{a:1,b:2} } }, 
      true 
  )
  </code></pre>
  
  <p>Will produce such error:</p>
  
  <blockquote>
    <p>Cannot apply $addToSet to a non-array field</p>
  </blockquote>
  
  <p>But after changed to:</p>
  
  <pre><code>db.test.update(
      {name:"abc123", "config.a":{$in:[1]}  }, 
      {$addToSet:{ config:{a:1,b:2} } }, 
      true 
  )
  </code></pre>
  
  <blockquote>
    <p>It works fine. </p>
  </blockquote>
  
  <p>Also referenced this link: <a href="https://stackoverflow.com/questions/14462962/meteor-mongodb-error-cannot-apply-addtoset-modifier-to-non-array">Answer</a></p>
  
  <p>Can Any one explain what's going on? <strong>"config.a":1</strong> will turn <strong>config</strong> to be an object? Where <strong>"config.a":{$in:[1]}</strong> won't?</p>