Mongo Update of Array Query using Upsert
<p>Given</p>

<ol>
<li>menuItems is a list ['foo', 'bar', 'fooz', 'ball'] and </li>
<li>menudb collection has 3 records: 'foo', 'bar', 'fooz'</li>
</ol>

<p>When I run</p>

<pre><code>menudb.update({"_id" : {"$in": menuItems}}, {"$addToSet": {"staleCount": 100}}, upsert=True)
</code></pre>

<p>Instead of creating a new record called 'ball', it creates a new record called 'ObjectId("57d730777bc6a465c9124111")'.</p>

<p>Is there a way to make the newly created record's '_id' to be that from the list?</p>

<p>=Thanks</p>

<p>you can remove upsert if you do not want new item in the menudb collection. I seems like your id is objectId instead of string.</p>

<p><a href="https://docs.mongodb.com/manual/reference/operator/query/in/" rel="nofollow">https://docs.mongodb.com/manual/reference/operator/query/in/</a></p>

<p>you can verify if the _id foo exits in collection 
menudb.update({"_id" : "foo"}, {"$addToSet": {"staleCount": 100})</p>

python mongodb