擅长:python、mysql、java
<pre><code>mycollection.find_one_and_update({"_id": mongo_id},
{"$set": {"newfield": "abc"}})
</code></pre>
<p>应该为你工作得很出色。如果没有id为<code>mongo_id</code>的文档,它将失败,除非您还使用<code>upsert=True</code>。默认情况下,这将返回旧文档。要获得新的,请通过<code>return_document=ReturnDocument.AFTER</code>。所有参数都在<a href="http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find_one_and_update" rel="noreferrer">the API</a>中描述。</p>
<p>在MongoDB 3.0中介绍了该方法。扩展到3.2、3.4和3.6。</p>