MongoEngine / MongoEngine/mongoengine

.to_json() didn't skip default values when query with .only().

Open
#1,544 4 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

here is the sample:

from mongoengine import Document, ListField, StringField, connect


class T(Document):
    a = StringField()
    b = StringField(default="B")
    c = ListField()

connect()
t1 = T(a='a')
t1.save()

t2 = T.objects(id=t1.id).only('a')
t2.to_json()
t2[0].to_json()

expect output:
>>> '"_id": {"$oid": "object id"}, "a": "a"}'
actual output:
>>> '"_id": {"$oid": "object id"}, "a": "a", "b": "B", "c": []}'

Maybe we should keep __only_fields(in created) as a default fields for to_mongo() ?
And I think to_json() should add fields kwargs to propagate for to_mongo().

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 by tracing the .only() query result through to_json() and to_mongo(), focusing on how __only_fields is created and propagated. Reproduce the sample, then verify that serialization omits the default b and c fields while retaining _id and a.

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.