From 47577f2f47cfc6974baa795ff217f29351f919e8 Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Fri, 9 Nov 2018 01:44:30 +0800 Subject: [PATCH] Update existing document #1899 --- mongoengine/document.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mongoengine/document.py b/mongoengine/document.py index 37d60628..4abecaa9 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -436,6 +436,13 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): with set_write_concern(collection, write_concern) as wc_collection: if force_insert: return wc_collection.insert_one(doc).inserted_id + # insert_one will provoke UniqueError alongside save does not + # therefore, it need to catch and call replace_one. + if '_id' in doc: + raw_object = wc_collection.find_one_and_replace( + {'_id': doc['_id']}, doc) + if raw_object: + return doc['_id'] object_id = wc_collection.insert_one(doc).inserted_id