MongoEngine / MongoEngine/mongoengine

upsert_one doesn't use defined id/primary_key type.

Open
#2,035 1 comment 1 reaction 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

Details

When we use upsert_one the defined id type field is ignored and MongoEngine generate a ObjectId.

How to reproduce:

from mongoengine import *

class D2(DynamicDocument):
    custom_id = UUIDField(required=True, primary_key=True, default=uuid4)
    label = StringField()
    value = IntField()

connect()
D2.objects(label='A').upsert_one(value=1)

print(type(D2.objects().get().id))

Current result:

<class 'bson.objectid.ObjectId'> is displayed

Expected result:

<class 'uuid.UUID'>

Version

mongoengine: '0.16.3'
pymongo: '3.7.2'
Python 2.7.15

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 upsert_one entry point and reproduce the example using a primary-key UUIDField on DynamicDocument. Trace how the upsert creates an ID and compare it with normal document creation. Done means the reproduced document has a uuid.UUID ID rather than a MongoDB ObjectId.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.