Cannot upsert document with "_id" specified
davidlatwe opened this issue · comments
Problem
Specifying document "_id" field in update will raise WriteError: Performing an update on the path '_id' would modify the immutable field '_id'
even the "_id" didn't change.
>>> col.update_one({"_id": "my-id"}, {"$set": {"_id": "my-id"}}, upsert=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...\montydb\collection.py", line 285, in update_one
updator = Updator(update, array_filters)
File "...\montydb\engine\update.py", line 80, in __init__
self.operations = OrderedDict(sorted(self.parser(spec).items()))
File "...\montydb\engine\update.py", line 150, in parser
raise WriteError(msg, code=66)
montydb.errors.WriteError: Performing an update on the path '_id' would modify the immutable field '_id'
Pymongo only raise this error when update document use different _id from filter document.
-
col.update_one({"_id": "some-id"}, {"$set": {"_id": "some-id", "foo": "barbar"}}, upsert=True)
Valid, filter and update both specified same _id.
-
col.update_one({"_id": "some-id"}, {"$set": {"_id": "other-id", "foo": "barbar"}}, upsert=True)
Invalid, _id not the same.
-
col.update_one({"foo": "bar"}, {"$set": {"_id": "some-id", "foo": "barbar"}}, upsert=True)
Maybe invalid if filter result document exists and _id is not the same.