Skip to content

Allow empty projection dictionary in find queries #418

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

Conversation

EtiennePelletier
Copy link

The current code that verifies pymongo query projections does not replicate MongoDB shell's behavior when the projection is an empty dictionary.

> db.test.find({}, {})
{ "_id" : ObjectId("5cec99eb81f850232d377fe1"), "value" : 0 }
{ "_id" : ObjectId("5cec99ec81f850232d377fe2"), "value" : 1 }
{ "_id" : ObjectId("5cec99ec81f850232d377fe3"), "value" : 2 }

PyMongo verifies if the projection is not None, but before converting it to a proper dictionary with  helpers._fields_list_to_dict, any projection whose boolean value is False gets replaced with {"id": 1}. In the case of an empty dictionary, this is annoying, as it prevents creating a projection variable with an empty dictionary and optionally adding values to it.

>>> list(coll.find({}, {}))
[{'_id': ObjectId('5cec99eb81f850232d377fe1')},
 {'_id': ObjectId('5cec99ec81f850232d377fe2')},
 {'_id': ObjectId('5cec99ec81f850232d377fe3')}]

Simple problematic use case:

# In the scope of a request
projection = {}
if not args['verbose']:
    projection['out'] = 0
return list(coll.find({}, projection))

Not so elegant workaround to bypass this problem:

coll.find({}, projection or None)

🤹‍♂️ 🐍

@behackett
Copy link
Member

I'm open to making this change, but since PyMongo has behaved this way for almost the entirety of its existence we should wait until PyMongo 4.0 and list it as a potentially backward breaking change.

@rathisekaran
Copy link

Tracking ticket - PYTHON-1853

@pooooodles
Copy link

pooooodles commented May 4, 2020

We are planning to make this change in PYTHON-1853 for PyMongo 4.0.

@pooooodles pooooodles closed this May 4, 2020
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

Successfully merging this pull request may close these issues.

4 participants