mongoengine
mongoengine copied to clipboard
upsert_one() on a missing document does an insert, not considering default field values
mongoengine==0.24.2 pymongo[srv]==4.2.0 MongoDB 5.0
Persisting a document lacks fields defined in the Document class:
class MyDocument(Document):
version = IntField(default=1)
foo = StringField(required=True)
bar = StringField(required=True)
Creating an instance when there is nothing to be updated yet, doesn't consider default field value it seems ( or my expectation of the "insert" functionality of "upsert_one" is plain wrong ):
MyDocument.objects({"foo":"Hello"}).upsert_one(foo="World",bar="ups")
Inspecting the collection I can find the new document instance, but it only has fields "foo" and "bar" but lacks "version" !?
Yet another quirk when you forget to supply required field values - validation is not performed and that field gets a None
value when reading it back from db after the "insert" is done.
MyDocument.objects({"foo":"Hello"}).upsert_one(foo="World") # "bar" is not supplied but still required
@manuel-koch RE the missing default, as a workaround, it's also possible to do:
MyDocument.objects({"foo":"Hello"}).upsert_one(foo="World", set_on_insert__version=1)