From 173a1c13977a979dcb66845a37b99831cde4d55c Mon Sep 17 00:00:00 2001 From: Mario HD Date: Sat, 26 Oct 2019 10:50:44 -0500 Subject: [PATCH 1/2] Use mongo projection to remove _id from responses --- laguinho/routes/datasets.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/laguinho/routes/datasets.py b/laguinho/routes/datasets.py index f578ce9..05bbe8d 100644 --- a/laguinho/routes/datasets.py +++ b/laguinho/routes/datasets.py @@ -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})) return jsonify(dsets) @datasets.route("/datasets/", 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 From 4a3558ca000d06fecd51f0a9e792a9f04aa7ddc8 Mon Sep 17 00:00:00 2001 From: Mario HD Date: Mon, 28 Oct 2019 11:56:08 -0600 Subject: [PATCH 2/2] Use better practices in Pymongo projection --- laguinho/routes/datasets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/laguinho/routes/datasets.py b/laguinho/routes/datasets.py index 05bbe8d..90aaa8f 100644 --- a/laguinho/routes/datasets.py +++ b/laguinho/routes/datasets.py @@ -29,11 +29,11 @@ def publish(): @datasets.route("/datasets", methods=['GET'], strict_slashes=False) def get_datasets(): - dsets = list(datasets_metadata.find({}, {'_id': 0})) + dsets = list(datasets_metadata.find({}, projection={'_id': False})) return jsonify(dsets) @datasets.route("/datasets/", methods=['GET'], strict_slashes=False) def get_datasets_by_name(name): - search_result = datasets_metadata.find_one_or_404({'name': name}, {'_id': 0}) + search_result = datasets_metadata.find_one_or_404({'name': name}, projection={'_id': False}) return jsonify(search_result), 200