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
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions laguinho/routes/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,17 @@ def publish():
result = dataset_metadata.load(request.json, unknown=EXCLUDE)
if dataset_exists(result):
return jsonify('Dataset already exists'), 409
datasets_metadata.insert_one(result)
del result['_id']
datasets_metadata.insert_one(result.copy())
return jsonify(result), 201


@datasets.route("/datasets", methods=['GET'], strict_slashes=False)
def get_datasets():
dsets = list(datasets_metadata.find())
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.

return jsonify(dsets)


@datasets.route("/datasets/<name>", methods=['GET'], strict_slashes=False)
def get_datasets_by_name(name):
search_result = datasets_metadata.find_one_or_404({'name': name})
del search_result['_id']
search_result = datasets_metadata.find_one_or_404({'name': name}, {'_id': 0})
return jsonify(search_result), 200