MongoEngine / MongoEngine/mongoengine

upsert_one() on a missing document does an insert, not considering default field values

Open
#2,694 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

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" !?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the QuerySet.upsert_one() entry point and reproduce the example using MongoEngine 0.24.2, PyMongo 4.2.0, and MongoDB 5.0. Compare the inserted document with the MyDocument default declaration; done means the behavior of upsert_one() on a missing document is defined and covered for the default version field.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.