diff --git a/notebooks/task-3.ipynb b/notebooks/task-3.ipynb new file mode 100644 index 0000000..f872c9c --- /dev/null +++ b/notebooks/task-3.ipynb @@ -0,0 +1,626 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "51f00fd715d6d40c", + "metadata": { + "collapsed": false + }, + "source": [ + "[Documentation](https://pymongo.readthedocs.io/en/stable/tutorial.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e643732fe0e2efd8", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:22:10.063608059Z", + "start_time": "2023-11-27T09:22:09.736035918Z" + }, + "collapsed": false + }, + "outputs": [], + "source": [ + "from pymongo import MongoClient\n", + "\n", + "\n", + "class DB:\n", + " def __init__(self):\n", + " self.client = MongoClient(\"mongodb://localhost:27017/\")\n", + " self.db = self.client[\"10Academy\"]\n", + "\n", + " def list_collections(self):\n", + " # Returns collections inside the database\n", + " return self.db.list_collection_names()\n", + "\n", + " def check_if_collection_exist(self, collection_name: str):\n", + " if not self.list_collections().__contains__(collection_name):\n", + " raise Exception(f\"Collection, {collection_name} not found.\")\n", + "\n", + " def insert_to_collection(self, collection_name, data):\n", + " self.check_if_collection_exist(collection_name)\n", + " collection = self.db[collection_name]\n", + " return collection.insert_one(data)\n", + "\n", + " def insert_many_to_collection(self, collection_name, data):\n", + " self.check_if_collection_exist(collection_name)\n", + " result = self.db[collection_name].insert_many(data)\n", + " return result.inserted_ids\n", + "\n", + " def find_all(self, collection_name):\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db[collection_name].find()\n", + "\n", + " def find(self, collection_name, key, value):\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db[collection_name].find({key: value})\n", + " \n", + " def find_by_id(self, collection_name, _id):\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db[collection_name].find\n", + "\n", + " def find_one(self, collection_name, key, value):\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db[collection_name].find_one({key: value})\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "8c767a1c", + "metadata": {}, + "outputs": [], + "source": [ + "class DBWithSchema:\n", + " def __init__(self) -> None:\n", + " self.client = MongoClient(\"mongodb://localhost:27017/\")\n", + " self.db = self.client[\"10Academy1\"]\n", + "\n", + " self.employee_validator = {\n", + " \"$jsonSchema\": {\n", + " \"bsonType\": \"object\",\n", + " \"required\": [\"name\", \"age\", \"company\"],\n", + " \"properties\": {\n", + " \"name\": {\n", + " \"bsonType\": \"string\",\n", + " \"description\": \"must be a string and is required\"\n", + " },\n", + " \"age\": {\n", + " \"bsonType\": \"number\",\n", + " \"description\": \"must be an number and is required\"\n", + " },\n", + " \"company\": {\n", + " \"bsonType\": \"objectId\",\n", + " \"description\": \"must be an objectId and is required\"\n", + " }\n", + " }\n", + " }\n", + " }\n", + " self.company_validator = {\n", + " \"$jsonSchema\": {\n", + " \"bsonType\": \"object\",\n", + " \"required\": [\"name\", \"country\", \"city\"],\n", + " \"properties\": {\n", + " \"name\": {\n", + " \"bsonType\": \"string\",\n", + " \"description\": \"must be a string and is required\"\n", + " },\n", + " \"city\": {\n", + " \"bsonType\": \"string\",\n", + " \"description\": \"must be an string and is required\"\n", + " },\n", + " \"country\": {\n", + " \"bsonType\": \"string\",\n", + " \"description\": \"must be an string and is required\"\n", + " }\n", + " }\n", + " }\n", + " }\n", + " try:\n", + " self.db.create_collection(\"employee\")\n", + " self.db.create_collection(\"company\")\n", + " except Exception as e:\n", + " print(e)\n", + "\n", + " self.db.command(\"collMod\", \"employee\", validator=self.employee_validator)\n", + " self.db.command(\"collMod\", \"company\", validator=self.company_validator)\n", + "\n", + " def list_collections(self):\n", + " return self.db.list_collection_names()\n", + " \n", + " def get_validation(self, collection_name: str) -> dict:\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db.get_collection(collection_name).options()\n", + " \n", + " def check_if_collection_exist(self, collection_name: str):\n", + " if not self.list_collections().__contains__(collection_name):\n", + " raise Exception(f\"Collection, {collection_name} not found.\")\n", + "\n", + " def insert_to_collection(self, collection_name, data):\n", + " self.check_if_collection_exist(collection_name)\n", + " collection = self.db[collection_name]\n", + " return collection.insert_one(data)\n", + "\n", + " def insert_many_to_collection(self, collection_name, data):\n", + " self.check_if_collection_exist(collection_name)\n", + " result = self.db[collection_name].insert_many(data)\n", + " return result.inserted_ids\n", + "\n", + " def find_all(self, collection_name):\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db[collection_name].find()\n", + "\n", + " def find(self, collection_name, key, value):\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db[collection_name].find({key: value})\n", + " \n", + " def find_by_id(self, collection_name, _id):\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db[collection_name].find\n", + "\n", + " def find_one(self, collection_name, key, value):\n", + " self.check_if_collection_exist(collection_name)\n", + " return self.db[collection_name].find_one({key: value})\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "id": "42edf846f8ff36b6", + "metadata": { + "collapsed": false + }, + "source": [ + "#### Initialize DB Class" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "initial_id", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:22:10.102209282Z", + "start_time": "2023-11-27T09:22:09.762921575Z" + }, + "collapsed": false + }, + "outputs": [], + "source": [ + "db = DB()" + ] + }, + { + "cell_type": "markdown", + "id": "67a77c0aa2951381", + "metadata": { + "collapsed": false + }, + "source": [ + "#### Get list of collections" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1943d67b90ef5345", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:22:10.139918225Z", + "start_time": "2023-11-27T09:22:09.771118477Z" + }, + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "db.list_collections()" + ] + }, + { + "cell_type": "markdown", + "id": "eff826746c1013d4", + "metadata": { + "collapsed": false + }, + "source": [ + "#### Insert record to collection" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "feda1ca67e1f08bf", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:22:10.174968485Z", + "start_time": "2023-11-27T09:22:09.840811255Z" + }, + "collapsed": false + }, + "outputs": [ + { + "ename": "Exception", + "evalue": "Collection, users not found.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m data \u001b[39m=\u001b[39m {\n\u001b[1;32m 2\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mJohn\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 3\u001b[0m }\n\u001b[0;32m----> 4\u001b[0m inserted_record \u001b[39m=\u001b[39m db\u001b[39m.\u001b[39;49minsert_to_collection(\u001b[39m'\u001b[39;49m\u001b[39musers\u001b[39;49m\u001b[39m'\u001b[39;49m, data)\n\u001b[1;32m 5\u001b[0m \u001b[39mprint\u001b[39m(inserted_record\u001b[39m.\u001b[39minserted_id)\n", + "Cell \u001b[0;32mIn[2], line 18\u001b[0m, in \u001b[0;36mDB.insert_to_collection\u001b[0;34m(self, collection_name, data)\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39minsert_to_collection\u001b[39m(\u001b[39mself\u001b[39m, collection_name, data):\n\u001b[0;32m---> 18\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mcheck_if_collection_exist(collection_name)\n\u001b[1;32m 19\u001b[0m collection \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdb[collection_name]\n\u001b[1;32m 20\u001b[0m \u001b[39mreturn\u001b[39;00m collection\u001b[39m.\u001b[39minsert_one(data)\n", + "Cell \u001b[0;32mIn[2], line 15\u001b[0m, in \u001b[0;36mDB.check_if_collection_exist\u001b[0;34m(self, collection_name)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcheck_if_collection_exist\u001b[39m(\u001b[39mself\u001b[39m, collection_name: \u001b[39mstr\u001b[39m):\n\u001b[1;32m 14\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlist_collections()\u001b[39m.\u001b[39m\u001b[39m__contains__\u001b[39m(collection_name):\n\u001b[0;32m---> 15\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mException\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mCollection, \u001b[39m\u001b[39m{\u001b[39;00mcollection_name\u001b[39m}\u001b[39;00m\u001b[39m not found.\u001b[39m\u001b[39m\"\u001b[39m)\n", + "\u001b[0;31mException\u001b[0m: Collection, users not found." + ] + } + ], + "source": [ + "data = {\n", + " \"name\": \"John\"\n", + "}\n", + "inserted_record = db.insert_to_collection('users', data)\n", + "print(inserted_record.inserted_id)\n" + ] + }, + { + "cell_type": "markdown", + "id": "a758e8de3c6b4947", + "metadata": { + "collapsed": false + }, + "source": [ + "#### Insert more than record to collection" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "e1613dd98803f815", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:22:10.210845641Z", + "start_time": "2023-11-27T09:22:09.841239357Z" + }, + "collapsed": false + }, + "outputs": [ + { + "ename": "Exception", + "evalue": "Collection, users not found.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 9\u001b[0m\n\u001b[1;32m 1\u001b[0m data \u001b[39m=\u001b[39m [{\n\u001b[1;32m 2\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mTom\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 3\u001b[0m },\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 7\u001b[0m }\n\u001b[1;32m 8\u001b[0m ]\n\u001b[0;32m----> 9\u001b[0m inserted_records \u001b[39m=\u001b[39m db\u001b[39m.\u001b[39;49minsert_many_to_collection(\u001b[39m'\u001b[39;49m\u001b[39musers\u001b[39;49m\u001b[39m'\u001b[39;49m, data)\n\u001b[1;32m 10\u001b[0m \u001b[39mprint\u001b[39m(inserted_records)\n", + "Cell \u001b[0;32mIn[2], line 23\u001b[0m, in \u001b[0;36mDB.insert_many_to_collection\u001b[0;34m(self, collection_name, data)\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39minsert_many_to_collection\u001b[39m(\u001b[39mself\u001b[39m, collection_name, data):\n\u001b[0;32m---> 23\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mcheck_if_collection_exist(collection_name)\n\u001b[1;32m 24\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdb[collection_name]\u001b[39m.\u001b[39minsert_many(data)\n\u001b[1;32m 25\u001b[0m \u001b[39mreturn\u001b[39;00m result\u001b[39m.\u001b[39minserted_ids\n", + "Cell \u001b[0;32mIn[2], line 15\u001b[0m, in \u001b[0;36mDB.check_if_collection_exist\u001b[0;34m(self, collection_name)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcheck_if_collection_exist\u001b[39m(\u001b[39mself\u001b[39m, collection_name: \u001b[39mstr\u001b[39m):\n\u001b[1;32m 14\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlist_collections()\u001b[39m.\u001b[39m\u001b[39m__contains__\u001b[39m(collection_name):\n\u001b[0;32m---> 15\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mException\u001b[39;00m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mCollection, \u001b[39m\u001b[39m{\u001b[39;00mcollection_name\u001b[39m}\u001b[39;00m\u001b[39m not found.\u001b[39m\u001b[39m\"\u001b[39m)\n", + "\u001b[0;31mException\u001b[0m: Collection, users not found." + ] + } + ], + "source": [ + "data = [{\n", + " \"name\": \"Tom\"\n", + "},\n", + " {\n", + " \"name\": \"Jane\",\n", + " \"age\": 33\n", + "}\n", + "]\n", + "inserted_records = db.insert_many_to_collection('users', data)\n", + "print(inserted_records)" + ] + }, + { + "cell_type": "markdown", + "id": "e902fb63fe9eca24", + "metadata": { + "collapsed": false + }, + "source": [ + "#### Find All records from collection " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cddcaee42da38732", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:22:10.211491539Z", + "start_time": "2023-11-27T09:22:09.863917702Z" + }, + "collapsed": false + }, + "outputs": [], + "source": [ + "import pprint\n", + "\n", + "records = db.find_all('users')\n", + "for record in records:\n", + " pprint.pprint(record)" + ] + }, + { + "cell_type": "markdown", + "id": "f7f5cb0be2c22957", + "metadata": { + "collapsed": false + }, + "source": [ + "#### Filter record from collection" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "af345ea0ccb0bbfd", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:22:10.215331714Z", + "start_time": "2023-11-27T09:22:09.908992355Z" + }, + "collapsed": false + }, + "outputs": [], + "source": [ + "results = db.find('users', 'name', 'Jane')\n", + "for result in results:\n", + " pprint.pprint(result)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "974526cb5e1ea0c4", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:22:10.235289519Z", + "start_time": "2023-11-27T09:22:09.909276711Z" + }, + "collapsed": false + }, + "outputs": [], + "source": [ + "results = db.find('users', 'name', 'Rix')\n", + "for result in results:\n", + " pprint.pprint(result)" + ] + }, + { + "cell_type": "markdown", + "id": "43a0d42c731246ea", + "metadata": { + "collapsed": false + }, + "source": [ + "#### Find one from collection" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6e65a86719e4dd5d", + "metadata": { + "ExecuteTime": { + "end_time": "2023-11-27T09:23:09.020008428Z", + "start_time": "2023-11-27T09:23:08.994515406Z" + }, + "collapsed": false + }, + "outputs": [], + "source": [ + "result = db.find_one('users', 'name', 'Jane')\n", + "pprint.pprint(result)" + ] + }, + { + "cell_type": "markdown", + "id": "37a9ad04", + "metadata": {}, + "source": [ + "#### Initialize DBWithSchemaClass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a44faa0420149106", + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "db_with_schema = DBWithSchema()" + ] + }, + { + "cell_type": "markdown", + "id": "076a3b3c", + "metadata": {}, + "source": [ + "#### View validators" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4f5c537", + "metadata": {}, + "outputs": [], + "source": [ + "import pprint\n", + "company_validator = db_with_schema.get_validation('company')\n", + "pprint.pprint(company_validator)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cc3e1f07", + "metadata": {}, + "outputs": [], + "source": [ + "employee_validator = db_with_schema.get_validation('employee')\n", + "pprint.pprint(employee_validator)" + ] + }, + { + "cell_type": "markdown", + "id": "59dc048c", + "metadata": {}, + "source": [ + "#### Create Company" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3240bb1a", + "metadata": {}, + "outputs": [], + "source": [ + "company_data = [\n", + " {\n", + " \"name\": \"Mercedes-Benz\",\n", + " \"city\": \"Stuttgart\",\n", + " \"country\": \"Germany\"\n", + " },\n", + " {\n", + " \"name\": \"Chevrolet\",\n", + " \"city\": \"Detroit\",\n", + " \"country\": \"United States\"\n", + " }\n", + "]\n", + "inserted_records = db_with_schema.insert_many_to_collection('company', company_data)\n", + "print(inserted_records)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22bedf96", + "metadata": {}, + "outputs": [], + "source": [ + "employee_data = [\n", + " {\n", + " \"name\": \"Jane\",\n", + " \"age\": 24,\n", + " \"company\": inserted_records[0]\n", + " },\n", + " {\n", + " \"name\": \"John\",\n", + " \"age\": 26,\n", + " \"company\": inserted_records[1]\n", + " },\n", + " {\n", + " \"name\": \"Amy\",\n", + " \"age\": 23,\n", + " \"company\": inserted_records[1]\n", + " },\n", + " {\n", + " \"name\": \"Jack\",\n", + " \"age\": 22,\n", + " \"company\": inserted_records[0]\n", + " },\n", + "]\n", + "inserted_employees = db_with_schema.insert_many_to_collection('employee', employee_data)\n", + "print(inserted_employees)" + ] + }, + { + "cell_type": "markdown", + "id": "4d71c56b", + "metadata": {}, + "source": [ + "#### Select all Employees" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "76099a40", + "metadata": {}, + "outputs": [], + "source": [ + "import pprint\n", + "\n", + "employees = db_with_schema.find_all('employee')\n", + "for employee in employees:\n", + " pprint.pprint(employee)" + ] + }, + { + "cell_type": "markdown", + "id": "85c56722", + "metadata": {}, + "source": [ + "#### Look UP" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf62050e", + "metadata": {}, + "outputs": [], + "source": [ + "lookup = [\n", + " {\n", + " \"$lookup\": {\n", + " \"from\": \"employee\",\n", + " \"localField\": \"_id\",\n", + " \"foreignField\": \"company\",\n", + " \"as\": \"employees\"\n", + " }\n", + " }\n", + "]\n", + "employees_with_company = db_with_schema.db.company.aggregate(lookup)\n", + "employees_list = list(employees_with_company)\n", + "for employee in employees_list:\n", + " pprint.pprint(employee)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d5ee3446", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/tests/q.py b/tests/q.py new file mode 100644 index 0000000..31a613f --- /dev/null +++ b/tests/q.py @@ -0,0 +1 @@ +import pandas \ No newline at end of file