Use mongo projection to remove _id from responses#70
Use mongo projection to remove _id from responses#70MarioHdpz wants to merge 2 commits intoOpenDevUFCG:mainfrom
Conversation
paulojbleitao
left a comment
There was a problem hiding this comment.
Thanks a lot!! We knew there was a better way to do it, but just didn't know enough about Mongo haha
laguinho/routes/datasets.py
Outdated
| for ds in dsets: | ||
| del ds['_id'] | ||
|
|
||
| dsets = list(datasets_metadata.find({}, {'_id': 0})) |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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.
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!