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

前端 未结 4 1101
孤独总比滥情好
孤独总比滥情好 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:38

    There is a new way to do it since version 0.9 (explained here):

    location = Location.objects(user_id=user_id).modify(upsert=True, new=True, set__point=point)
    

    It returns the created or updated object.

提交回复
热议问题