擅长:python、mysql、java
<p>您不需要首先使用检索文档,只需使用带<a href="http://docs.mongodb.org/manual/reference/operator/update/push/" rel="noreferrer">^{<cd2>}</a>运算符的<code>.update</code>方法。</p>
<pre><code>def update_tags(ref, new_tag):
coll.update({'ref': ref}, {'$push': {'tags': new_tag}})
</code></pre>
<p>由于不推荐使用update,如果您使用的是pymongo 2.9或更新版本,那么应该使用<a href="http://api.mongodb.org/python/2.9/api/pymongo/collection.html#pymongo.collection.Collection.find_one_and_update" rel="noreferrer">^{<cd3>}</a>或<a href="http://api.mongodb.org/python/2.9/api/pymongo/collection.html#pymongo.collection.Collection.update_one" rel="noreferrer">^{<cd4>}</a>方法</p>