MongoEngine: Replacing get_or_create with upsert/update_one

孤者浪人 提交于 2019-12-06 06:49:07
Ross

You are very close but you need to use a findAndModify command via modify rather than an update command.

NewDoc = Model.objects(first_name='John', 
                       last_name='Potter', 
                       age=40).modify(upsert=True, new=True,
                                      set__first_name='John, 
                                      set__last_name='Potter', 
                                      set__age=40,
                                      set_on_insert__newUser=True)

Take note of the first 2 modify kwargs - upsert and new. Also take note of the $setOnInsert operator example which will only set a field if the findAndModify does an upsert.

Germano

You should look at modify. Passing a new=True you'll get the updated object (or document, in mongodb parlance).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!