擅长:python、mysql、java
<p>注意,<code>get_or_create</code>现在被安排为不推荐使用,因为在MongoDB中没有事务支持,它无法确保原子性。</p>
<p>首选方法是<a href="http://mongoengine-odm.readthedocs.org/en/latest/apireference.html#mongoengine.queryset.QuerySet.update">update</a>加upsert:</p>
<pre><code>Location.objects(user_id=user_id).update_one(set__point=point, upsert=True)
</code></pre>
<p>更多关于<a href="http://docs.mongodb.org/manual/core/update/#update-operations-with-the-upsert-flag">MongoDB documentation</a>上upserts的信息。</p>