How to do “insert if not exist else update” with mongoengine?

前端 未结 4 1100
孤独总比滥情好
孤独总比滥情好 2020-12-14 17:56

I\'m working with mongoengine in Django,
this is my document defination:

class Location(mongoengine.Document):  
    user_id = mongoengine.IntField(req         


        
4条回答
  •  一个人的身影
    2020-12-14 18:46

    Note that get_or_create is now scheduled to be deprecated, because with no transaction support in MongoDB it cannot ensure atomicity.

    The preferred way is update with upsert:

    Location.objects(user_id=user_id).update_one(set__point=point, upsert=True)
    

    More on upserts on the MongoDB documentation.

提交回复
热议问题