def update_many(self, payload): content = request.get_json(force=True, silent=True) col = get_collection(payload['name'], content["collection"]) if not col: return self.response.response_err(404, "collection not exist") options = options_filter(content, ("upsert", "bypass_document_validation")) try: update_set_on_insert = content.get('update').get( '$setOnInsert', None) if update_set_on_insert: content["update"]["$setOnInsert"]['created'] = datetime.utcnow( ) else: content["update"]["$setOnInsert"] = { "created": datetime.utcnow() } if "$set" in content["update"]: content["update"]["$set"]["modified"] = datetime.utcnow() ret = col.update_many(convert_oid(content["filter"]), convert_oid(content["update"], update=True), **options) data = { "acknowledged": ret.acknowledged, "matched_count": ret.matched_count, "modified_count": ret.modified_count, "upserted_id": str(ret.upserted_id) } return self.response.response_ok(data) except Exception as e: return self.response.response_err(500, "Exception:" + str(e))
def insert_many(self, payload): content = request.get_json(force=True, silent=True) col = get_collection(payload['name'], content["collection"]) if not col: return self.response.response_err(404, "collection not exist") options = options_filter(content, ("bypass_document_validation", "ordered")) try: new_document = [] for document in content["document"]: document["created"] = datetime.utcnow() document["modified"] = datetime.utcnow() new_document.append(convert_oid(document)) ret = col.insert_many(new_document, **options) data = { "acknowledged": ret.acknowledged, "inserted_ids": [str(_id) for _id in ret.inserted_ids] } return self.response.response_ok(data) except Exception as e: return self.response.response_err(500, "Exception:" + str(e))
def delete_one(self, payload): content = request.get_json(force=True, silent=True) col = get_collection(payload['name'], content["collection"]) if not col: return self.response.response_err(404, "collection not exist") data, err_message = query_delete_one(col, content) if err_message: return self.response.response_err(500, err_message) return self.response.response_ok(data)
def count_documents(self, payload): content = request.get_json(force=True, silent=True) options = populate_options_count_documents(content) col = get_collection(payload['name'], content["collection"]) if not col: return self.response.response_err(404, "collection not exist") data, err_message = query_count_documents(col, content, options) if err_message: return self.response.response_err(500, err_message) return self.response.response_ok(data)
def find_many(self, payload): content = request.get_json(force=True, silent=True) options = populate_options_find_many(content) col = get_collection(payload['name'], content.get('collection')) if not col: return self.response.response_err(404, "collection not exist") data, err_message = query_find_many(col, content, options) if err_message: return self.response.response_err(500, err_message) return self.response.response_ok(data)
def delete_many(self, payload): content = request.get_json(force=True, silent=True) col = get_collection(payload['name'], content["collection"]) if not col: return self.response.response_err(404, "collection not exist") try: ret = col.delete_many(convert_oid(content["filter"])) data = { "acknowledged": ret.acknowledged, "deleted_count": ret.deleted_count, } return self.response.response_ok(data) except Exception as e: return self.response.response_err(500, "Exception:" + str(e))
def find_one(self, payload): content = request.get_json(force=True, silent=True) col = get_collection(payload['name'], content["collection"]) if not col: return self.response.response_err(404, "collection not exist") options = options_filter( content, ("projection", "skip", "sort", "allow_partial_results", "return_key", "show_record_id", "batch_size")) if "sort" in options: sorts = gene_sort(options["sort"]) options["sort"] = sorts try: if "filter" in content: result = col.find_one(convert_oid(content["filter"]), **options) else: result = col.find_one(**options) data = {"items": json.loads(json_util.dumps(result))} return self.response.response_ok(data) except Exception as e: return self.response.response_err(500, "Exception:" + str(e))