Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only performed after a hint doesn't work #57

Open
wojcikstefan opened this issue Jan 17, 2025 · 0 comments
Open

only performed after a hint doesn't work #57

wojcikstefan opened this issue Jan 17, 2025 · 0 comments

Comments

@wojcikstefan
Copy link
Member

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'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant