Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Use mongo projection to remove _id from responses #70

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

MarioHdpz
Copy link

Enhancement proposal for issue #69

Mongo allow us to select the fields we're retreiving by providing a second argument (projection).

So I added the projection {_id: 0} to prevent that field to appear in responses instead of deleting it from the response.

One interesting thing is that the insert_one method from PyMongo was modifying the input we were providing, so I prevent this mutation by providing a copy of the result dictionary.

Hope it helps!

Copy link
Contributor

@paulojbleitao paulojbleitao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!! We knew there was a better way to do it, but just didn't know enough about Mongo haha

for ds in dsets:
del ds['_id']

dsets = list(datasets_metadata.find({}, {'_id': 0}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a quick look at the documentation for this, would .find(projection={'_id': 0}) work as well? Just so we can avoid passing an empty dictionary whose meaning isn't very clear.

Also, why 0? I saw that in PyMongo's documentation they use False instead. I think it's clearer to use a boolean rather than an integer in this case, if I understood correctly 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there, I've just seen this message.

Sure, python allows you to pass it as positional or keyword argument.

And I agree with you about the False, I'm just too used to do it with zeros, but I've never read that part of Pymongo docs.

I will update the PR with these changes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants