我需要知道是否插入了一個upsert,使用MongoEngine(或者如果有必要,pymongo)。如何判斷我是否在沒有(不建議使用的)get_or_create的情況下在mongoengine的upsert中插入?
的代碼看起來是這樣的:
ret = MyMongoCollection.objects(name=desiredname)
.update_one({ upsert: True, field1: 2 })
Mongoengine似乎只返回 「num_affected」 這始終是正好1這裏,顧名思義。
我知道這些存在,但我正在尋找Python的味道。
- How to do "insert if not exist else update" with mongoengine?
- Check if MongoDB upsert did an insert or an update