Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.
This repository was archived by the owner on Dec 1, 2025. It is now read-only.

only performed after a hint doesn't work #57

@wojcikstefan

Description

@wojcikstefan

only(...) is used on a QuerySet to limit which fields are pulled from the database. It generally works as expected... unless it happens to follow a hint(...).

You can reproduce the bug with the following setup:

In [1]: from mongoengine import *

In [2]: connect("testdb")
Out[2]: MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary())

In [3]: class SomeDoc(Document):
   ...:     field_a = StringField()
   ...:     field_b = StringField()
   ...:

In [4]: SomeDoc.objects.create(field_a="A", field_b="B")
Out[4]: <SomeDoc: SomeDoc object>

Then, you can see that hint(...) following only(...) works fine while only(...) following a hint(...) does not:

In [6]: SomeDoc.objects.all()[0]._db_data  # Not specifying `only` works as expected, returning whole documents.
Out[6]: {'_id': ObjectId('678a3981aa013edfb99cf2c5'), 'field_a': 'A', 'field_b': 'B'}

In [7]: SomeDoc.objects.only("field_a")[0]._db_data  # Specifying `only` on its own works as expected, returning partial documents.
Out[7]: {'_id': ObjectId('678a3981aa013edfb99cf2c5'), 'field_a': 'A'}

In [11]: SomeDoc.objects.hint([("_id", 1)]).only("field_a")[0]._db_data  # `only` following a `hint` is broken, returning whole documents instead of partial documents.
Out[11]: {'_id': ObjectId('678a3981aa013edfb99cf2c5'), 'field_a': 'A', 'field_b': 'B'}

In [12]: SomeDoc.objects.only("field_a").hint([("_id", 1)])[0]._db_data  # `hint` following `only` works fine, returning partial documents.
Out[12]: {'_id': ObjectId('678a3981aa013edfb99cf2c5'), 'field_a': 'A'}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions