├── Part 1 └── docker-compose.yml ├── Part 2 └── master.py ├── Part 3 └── MongoDB.ipynb ├── README.md └── part4 └── queryGenerator.py /Part 1/docker-compose.yml: -------------------------------------------------------------------------------- 1 | # on 2 | version: '3.7' 3 | services: 4 | mongodb_container: 5 | image: mongo:latest 6 | environment: 7 | MONGO_INITDB_ROOT_USERNAME: root 8 | MONGO_INITDB_ROOT_PASSWORD: rootpassword 9 | ports: 10 | - 27017:27017 11 | volumes: 12 | - /Tutorial:/data/db -------------------------------------------------------------------------------- /Part 2/master.py: -------------------------------------------------------------------------------- 1 | __NAME__ = "Soumil Nitin Shah " 2 | __VERSION__ = "0.0.1" 3 | 4 | 5 | try: 6 | import os 7 | import pandas as pd 8 | import sys 9 | import io 10 | import pymongo 11 | import json 12 | from pymongo import MongoClient 13 | from bson.objectid import ObjectId 14 | print("All Modules loaded ") 15 | except Exception as e: 16 | print("Error : {} ".format(e)) 17 | 18 | 19 | class Singleton(type): 20 | 21 | """This would be a Singleton Design pattern """ 22 | 23 | _instance = {} 24 | 25 | def __call__(cls, *args, **kwargs): 26 | """Creates one one instance of the class """ 27 | if cls not in cls._instance: 28 | cls._instance[cls] = super(Singleton, cls).__call__(*args, **kwargs) 29 | return cls._instance[cls] 30 | 31 | 32 | class Settings(metaclass=Singleton): 33 | def __init__(self, host=None, maxPoolSize=50, port=27010): 34 | self.host = host 35 | self.maxPoolSize = maxPoolSize 36 | self.port = port 37 | 38 | 39 | class Client(metaclass=Singleton): 40 | 41 | def __init__(self, _settings=None): 42 | self.settings = _settings 43 | self.mclient = MongoClient(host=self.settings.host, 44 | port=self.settings.port, 45 | maxPoolSize=self.settings.maxPoolSize) 46 | 47 | 48 | class MongoInsert(object): 49 | 50 | def __init__(self, _client=None, dbName=None, collectionName=None): 51 | self.client = _client 52 | self.dbName=dbName 53 | self.collectionName=collectionName 54 | 55 | def insert_one(self, record={}): 56 | """ 57 | Insert record in Mongo DB 58 | :param record: Json 59 | :return: Bool 60 | """ 61 | try: 62 | self.client.mclient[self.dbName][self.collectionName].insert_one(record) 63 | return True 64 | except Exception as e: 65 | print("Error : {} ".format(e)) 66 | return False 67 | 68 | def insert_pandas_df(self,df=None): 69 | """ 70 | 71 | :param df: Pandas DF 72 | :return: Bool 73 | """ 74 | 75 | try: 76 | self.client.mclient[self.dbName][self.collectionName].insert_many(df.to_dict(), ordered=False) 77 | return True 78 | except Exception as e: 79 | return False 80 | 81 | 82 | class Mongoinformation(object): 83 | 84 | def __init__(self, _client=None): 85 | self.client = _client 86 | 87 | def getALllDatabase(self): 88 | """ 89 | Rreturn all Database Name in Mongo Db 90 | :return: List 91 | """ 92 | return self.client.mclient.list_database_names() 93 | 94 | def getAllCollections(self, DbName=None): 95 | """ 96 | 97 | :param DbName: Str 98 | :return: List 99 | """ 100 | if DbName is None: 101 | return [] 102 | else: 103 | self.client.mclient[DbName].list_collection_names() 104 | 105 | 106 | class Mongoose(object): 107 | 108 | """facade Class """ 109 | 110 | def __init__(self, host=None,port=27010, maxPoolSize=50,dbName=None, collectionName=None): 111 | self._settings =Settings(host=host, port=port, maxPoolSize=maxPoolSize) # Cretes a instance of settings class 112 | self.client = Client(_settings=self._settings) # creates a single instance of client object 113 | self.dbName=dbName 114 | self.collectionNam = collectionName 115 | self._insert = MongoInsert(_client=self.client, dbName=self.dbName,collectionName=self.collectionNam) 116 | 117 | def insert_one_record(self, record): 118 | return self._insert.insert_one(record=record) 119 | 120 | 121 | def main1(): 122 | _helper = Mongoose(host="mongodb://root:rootpassword@localhost:27017",dbName='person',collectionName='names') 123 | _record = {"name":"Nitin shah"} 124 | _helper.insert_one_record(record=_record) 125 | 126 | 127 | # 128 | # def main(): 129 | # 130 | # url = "mongodb://root:rootpassword@localhost:27017" 131 | # 132 | # # Cretes a instance of settings class 133 | # _settings = Settings(host=url) 134 | # 135 | # # creates a single instance of client object 136 | # _client = Client(_settings=_settings) 137 | # 138 | # _record = {"name":"Soumil shah"} 139 | # dbName= "person" 140 | # collectionName = "names" 141 | # 142 | # _helper = MongoInsert(_client=_client, dbName=dbName,collectionName=collectionName) 143 | # res = _helper.insert_one(record=_record) 144 | # print(res) 145 | 146 | 147 | if __name__ == "__main__": 148 | main1() 149 | 150 | -------------------------------------------------------------------------------- /Part 3/MongoDB.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Learning MongoDb in a easy Way " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "##### Basics \n", 15 | "* Database Name - > Database Name \n", 16 | "* Table Name -> collection Name " 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "!pip install pymongo" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 1, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "All Modules loaded \n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "try:\n", 43 | " import os\n", 44 | " import pandas as pd\n", 45 | " import sys\n", 46 | " import io\n", 47 | " import pymongo\n", 48 | " import json\n", 49 | " from pymongo import MongoClient\n", 50 | " from bson.objectid import ObjectId\n", 51 | "\n", 52 | " print(\"All Modules loaded \")\n", 53 | "except Exception as e:\n", 54 | " print(\"Error : {} \".format(e))" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "* Before you start working understanding basics is important. Now that you understand what a database Name is amd what a collection is we will use pymongo Library for this. remember i also have a video if you want to develop helper classes on top of pyongo i have a video on how to do that \n" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "###### Client object \n", 69 | "* Always We start by creating a client object " 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "# Par 1:\n", 77 | "##### Installation of Mongo DB using Docker \n", 78 | "* https://www.youtube.com/watch?v=u6bOt3rp-Oo\n", 79 | "\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "* mongodb://root:rootpassword@localhost:27017\n", 87 | "\n", 88 | "version: '3.7'\n", 89 | "\n", 90 | "services:\n", 91 | "\n", 92 | " mongodb_container:\n", 93 | " \n", 94 | " image: mongo:latest\n", 95 | " \n", 96 | " environment:\n", 97 | " \n", 98 | " MONGO_INITDB_ROOT_USERNAME: root\n", 99 | " \n", 100 | " MONGO_INITDB_ROOT_PASSWORD: rootpassword\n", 101 | " \n", 102 | " ports:\n", 103 | " \n", 104 | " - 27017:27017\n", 105 | " \n", 106 | " volumes:\n", 107 | " \n", 108 | " - /Tutorial:/data/db" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 2, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "CONNECTION_URL = \"mongodb://root:rootpassword@localhost:27017\"" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 3, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "client = MongoClient(host=CONNECTION_URL)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "##### Getting all DB Names " 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 4, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "data": { 143 | "text/plain": [ 144 | "['admin', 'config', 'local', 'person']" 145 | ] 146 | }, 147 | "execution_count": 4, 148 | "metadata": {}, 149 | "output_type": "execute_result" 150 | } 151 | ], 152 | "source": [ 153 | "client.list_database_names()" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "###### Getting all collection Names " 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 9, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "DBNAME = 'person'" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 13, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "['names']" 181 | ] 182 | }, 183 | "execution_count": 13, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "client[DBNAME].list_collection_names()" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "### Insert operations " 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "* When you wnat to insert Items in MongoDB you always have to say which Database Name and which collection Name " 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "* inserting one Documents" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "* Lets Create a new Database with Name Languages and have a table name language" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 63, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "data": { 227 | "text/plain": [ 228 | "" 229 | ] 230 | }, 231 | "execution_count": 63, 232 | "metadata": {}, 233 | "output_type": "execute_result" 234 | } 235 | ], 236 | "source": [ 237 | "client['Languages']['language'].insert_one({\n", 238 | " \n", 239 | " \"name\":\"Soumil Nitn Shah \",\n", 240 | " \"age\":25,\n", 241 | " \"language\":[\"Python\", \"c#\" , \"c++\"]\n", 242 | " \n", 243 | " \n", 244 | "})" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [ 251 | "* What Mongo Does it will insert this Record with a Unique Key ie Primary Key " 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "* Inserting with unique Identifier you specify " 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 64, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "text/plain": [ 269 | "" 270 | ] 271 | }, 272 | "execution_count": 64, 273 | "metadata": {}, 274 | "output_type": "execute_result" 275 | } 276 | ], 277 | "source": [ 278 | "client['Languages']['language'].insert_one({\n", 279 | " \"_id\":\"1\",\n", 280 | " \"name\":\" Nitn Shah \",\n", 281 | " \"age\":57,\n", 282 | " \"language\":[\"Python\", \"c#\" , \"c++\", \"java\"]\n", 283 | " \n", 284 | " \n", 285 | "})" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "* Insert many " 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 65, 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "data = [{\n", 302 | " \"_id\":\"2\",\n", 303 | " \"name\":\" Suhas Shah \",\n", 304 | " \"age\":60,\n", 305 | " \"language\":[\"Python\", \"java\"]\n", 306 | " \n", 307 | " \n", 308 | "},\n", 309 | "{\n", 310 | " \"_id\":\"3\",\n", 311 | " \"name\":\" Nitn Shah \",\n", 312 | " \"age\":57,\n", 313 | " \"language\":[\"Python\", \"java\"]\n", 314 | " \n", 315 | " \n", 316 | "}]" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 66, 322 | "metadata": {}, 323 | "outputs": [ 324 | { 325 | "data": { 326 | "text/plain": [ 327 | "" 328 | ] 329 | }, 330 | "execution_count": 66, 331 | "metadata": {}, 332 | "output_type": "execute_result" 333 | } 334 | ], 335 | "source": [ 336 | "client['Languages']['language'].insert_many(data)" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "* Understanding to insert pandas Dataframe " 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 67, 349 | "metadata": {}, 350 | "outputs": [], 351 | "source": [ 352 | "data = [{\n", 353 | " \"_id\":\"4\",\n", 354 | " \"name\":\" karan Shah \",\n", 355 | " \"age\":60,\n", 356 | " \"language\":[\"Python\", \"java\"]\n", 357 | " \n", 358 | " \n", 359 | "},\n", 360 | "{\n", 361 | " \"_id\":\"5\",\n", 362 | " \"name\":\" Shah \",\n", 363 | " \"age\":57,\n", 364 | " \"language\":[\"Python\", \"java\"]\n", 365 | " \n", 366 | " \n", 367 | "}]\n", 368 | "\n", 369 | "import pandas as pd\n", 370 | "df = pd.DataFrame(data=data)" 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 68, 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "data": { 380 | "text/html": [ 381 | "
\n", 382 | "\n", 395 | "\n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | "
_idnameagelanguage
04karan Shah60[Python, java]
15Shah57[Python, java]
\n", 422 | "
" 423 | ], 424 | "text/plain": [ 425 | " _id name age language\n", 426 | "0 4 karan Shah 60 [Python, java]\n", 427 | "1 5 Shah 57 [Python, java]" 428 | ] 429 | }, 430 | "execution_count": 68, 431 | "metadata": {}, 432 | "output_type": "execute_result" 433 | } 434 | ], 435 | "source": [ 436 | "df.head()" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 69, 442 | "metadata": {}, 443 | "outputs": [ 444 | { 445 | "data": { 446 | "text/plain": [ 447 | "" 448 | ] 449 | }, 450 | "execution_count": 69, 451 | "metadata": {}, 452 | "output_type": "execute_result" 453 | } 454 | ], 455 | "source": [ 456 | "client['Languages']['language'].insert_many(df.to_dict('record'), ordered=False)" 457 | ] 458 | }, 459 | { 460 | "cell_type": "markdown", 461 | "metadata": {}, 462 | "source": [ 463 | "#### Update if Record Exists " 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": 70, 469 | "metadata": {}, 470 | "outputs": [], 471 | "source": [ 472 | "new = {\n", 473 | " \"name\":\"Hacked\",\n", 474 | " \"age\":222,\n", 475 | " \"language\":[\"python3\"]\n", 476 | " \n", 477 | "}" 478 | ] 479 | }, 480 | { 481 | "cell_type": "code", 482 | "execution_count": 71, 483 | "metadata": {}, 484 | "outputs": [ 485 | { 486 | "data": { 487 | "text/plain": [ 488 | "{'_id': '4', 'name': ' karan Shah ', 'age': 60, 'language': ['Python', 'java']}" 489 | ] 490 | }, 491 | "execution_count": 71, 492 | "metadata": {}, 493 | "output_type": "execute_result" 494 | } 495 | ], 496 | "source": [ 497 | "client['Languages']['language'].find_one_and_update(\n", 498 | "{\"_id\":\"4\"},\n", 499 | "{\"$set\": new}\n", 500 | ")" 501 | ] 502 | }, 503 | { 504 | "cell_type": "markdown", 505 | "metadata": {}, 506 | "source": [ 507 | "* what if you just want to change one attribute " 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": 72, 513 | "metadata": {}, 514 | "outputs": [ 515 | { 516 | "data": { 517 | "text/plain": [ 518 | "{'_id': '4', 'name': 'Hacked', 'age': 222, 'language': ['python3']}" 519 | ] 520 | }, 521 | "execution_count": 72, 522 | "metadata": {}, 523 | "output_type": "execute_result" 524 | } 525 | ], 526 | "source": [ 527 | "client['Languages']['language'].find_one_and_update(\n", 528 | "{\"_id\":\"4\"},\n", 529 | "{\"$set\": {\"name\":\"HMMM CHNAGE ME \"}}\n", 530 | ")" 531 | ] 532 | }, 533 | { 534 | "cell_type": "markdown", 535 | "metadata": {}, 536 | "source": [ 537 | "* remember if it dosent find the record with Search filter you profvide it will insert a new one " 538 | ] 539 | }, 540 | { 541 | "cell_type": "markdown", 542 | "metadata": {}, 543 | "source": [ 544 | "#### Search " 545 | ] 546 | }, 547 | { 548 | "cell_type": "markdown", 549 | "metadata": {}, 550 | "source": [ 551 | "* match all " 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 73, 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "data": { 561 | "text/plain": [ 562 | "" 563 | ] 564 | }, 565 | "execution_count": 73, 566 | "metadata": {}, 567 | "output_type": "execute_result" 568 | } 569 | ], 570 | "source": [ 571 | "client['Languages']['language'].find({})" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": {}, 577 | "source": [ 578 | "* Remeber it will always give you a generator Object to get the data you need a iterator\n", 579 | "{} mean find everything " 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 74, 585 | "metadata": {}, 586 | "outputs": [ 587 | { 588 | "name": "stdout", 589 | "output_type": "stream", 590 | "text": [ 591 | "{'_id': ObjectId('5f80fc12651cf487416590c0'), 'name': 'Soumil Nitn Shah ', 'age': 25, 'language': ['Python', 'c#', 'c++']}\n" 592 | ] 593 | } 594 | ], 595 | "source": [ 596 | "for x in client['Languages']['language'].find({}):\n", 597 | " print(x)\n", 598 | " break" 599 | ] 600 | }, 601 | { 602 | "cell_type": "markdown", 603 | "metadata": {}, 604 | "source": [ 605 | "* less than operator " 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": 75, 611 | "metadata": {}, 612 | "outputs": [ 613 | { 614 | "name": "stdout", 615 | "output_type": "stream", 616 | "text": [ 617 | "{'_id': ObjectId('5f80fc12651cf487416590c0'), 'name': 'Soumil Nitn Shah ', 'age': 25, 'language': ['Python', 'c#', 'c++']}\n", 618 | "{'_id': '1', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'c#', 'c++', 'java']}\n", 619 | "{'_id': '3', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'java']}\n", 620 | "{'_id': '5', 'name': ' Shah ', 'age': 57, 'language': ['Python', 'java']}\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "for x in client['Languages']['language'].find({\n", 626 | " \"age\": {\n", 627 | " \"$lt\": 60}\n", 628 | "}):\n", 629 | " print(x)" 630 | ] 631 | }, 632 | { 633 | "cell_type": "markdown", 634 | "metadata": {}, 635 | "source": [ 636 | "* Searching in array " 637 | ] 638 | }, 639 | { 640 | "cell_type": "markdown", 641 | "metadata": {}, 642 | "source": [ 643 | "##### AND" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": 76, 649 | "metadata": {}, 650 | "outputs": [ 651 | { 652 | "name": "stdout", 653 | "output_type": "stream", 654 | "text": [ 655 | "{'_id': '2', 'name': ' Suhas Shah ', 'age': 60, 'language': ['Python', 'java']}\n", 656 | "{'_id': '3', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'java']}\n", 657 | "{'_id': '5', 'name': ' Shah ', 'age': 57, 'language': ['Python', 'java']}\n" 658 | ] 659 | } 660 | ], 661 | "source": [ 662 | "for x in client['Languages']['language'].find({\n", 663 | " 'language':['Python', 'java']\n", 664 | "}):\n", 665 | " print(x)" 666 | ] 667 | }, 668 | { 669 | "cell_type": "markdown", 670 | "metadata": {}, 671 | "source": [ 672 | "#### OR" 673 | ] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "execution_count": 52, 678 | "metadata": {}, 679 | "outputs": [ 680 | { 681 | "name": "stdout", 682 | "output_type": "stream", 683 | "text": [ 684 | "{'_id': '1', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'c#', 'c++', 'java']}\n", 685 | "{'_id': '2', 'name': ' Suhas Shah ', 'age': 60, 'language': ['Python', 'java']}\n", 686 | "{'_id': '3', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'java']}\n", 687 | "{'_id': '5', 'name': ' Shah ', 'age': 57, 'language': ['Python', 'java']}\n" 688 | ] 689 | } 690 | ], 691 | "source": [ 692 | "for x in client['Languages']['language'].find({\n", 693 | " \"$or\":[\n", 694 | " {\n", 695 | " \"language\":\"python\"\n", 696 | " },\n", 697 | " {\n", 698 | " \"language\":\"java\"\n", 699 | " }\n", 700 | " ]\n", 701 | "}):\n", 702 | " print(x)" 703 | ] 704 | }, 705 | { 706 | "cell_type": "markdown", 707 | "metadata": {}, 708 | "source": [ 709 | "#### Sort" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": 54, 715 | "metadata": {}, 716 | "outputs": [ 717 | { 718 | "name": "stdout", 719 | "output_type": "stream", 720 | "text": [ 721 | "{'_id': '5', 'name': ' Shah ', 'age': 57, 'language': ['Python', 'java']}\n", 722 | "{'_id': '1', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'c#', 'c++', 'java']}\n", 723 | "{'_id': '3', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'java']}\n", 724 | "{'_id': '2', 'name': ' Suhas Shah ', 'age': 60, 'language': ['Python', 'java']}\n" 725 | ] 726 | } 727 | ], 728 | "source": [ 729 | " data = client['Languages']['language'].find({\n", 730 | " \"$or\":[\n", 731 | " {\n", 732 | " \"language\":\"python\"\n", 733 | " },\n", 734 | " {\n", 735 | " \"language\":\"java\"\n", 736 | " }\n", 737 | " ]\n", 738 | "}).sort(\"name\")\n", 739 | "\n", 740 | "for x in data:\n", 741 | " print(x)" 742 | ] 743 | }, 744 | { 745 | "cell_type": "code", 746 | "execution_count": 56, 747 | "metadata": { 748 | "scrolled": true 749 | }, 750 | "outputs": [ 751 | { 752 | "name": "stdout", 753 | "output_type": "stream", 754 | "text": [ 755 | "{'_id': '5', 'name': ' Shah ', 'age': 57, 'language': ['Python', 'java']}\n", 756 | "{'_id': '1', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'c#', 'c++', 'java']}\n", 757 | "{'_id': '3', 'name': ' Nitn Shah ', 'age': 57, 'language': ['Python', 'java']}\n", 758 | "{'_id': '2', 'name': ' Suhas Shah ', 'age': 60, 'language': ['Python', 'java']}\n" 759 | ] 760 | } 761 | ], 762 | "source": [ 763 | " data = client['Languages']['language'].find({\n", 764 | " \"$or\":[\n", 765 | " {\n", 766 | " \"language\":\"python\"\n", 767 | " },\n", 768 | " {\n", 769 | " \"language\":\"java\"\n", 770 | " }\n", 771 | " ]\n", 772 | "}).sort(\"name\",1)\n", 773 | "\n", 774 | "for x in data:\n", 775 | " print(x)" 776 | ] 777 | }, 778 | { 779 | "cell_type": "markdown", 780 | "metadata": {}, 781 | "source": [ 782 | "* sort(\"name\", 1) #ascending\n", 783 | "* sort(\"name\", -1) #descending" 784 | ] 785 | }, 786 | { 787 | "cell_type": "markdown", 788 | "metadata": {}, 789 | "source": [ 790 | "##### delete " 791 | ] 792 | }, 793 | { 794 | "cell_type": "code", 795 | "execution_count": 60, 796 | "metadata": {}, 797 | "outputs": [ 798 | { 799 | "data": { 800 | "text/plain": [ 801 | "" 802 | ] 803 | }, 804 | "execution_count": 60, 805 | "metadata": {}, 806 | "output_type": "execute_result" 807 | } 808 | ], 809 | "source": [ 810 | "client['Languages']['language'].delete_one({\"name\":\"Shah \"})" 811 | ] 812 | }, 813 | { 814 | "cell_type": "markdown", 815 | "metadata": {}, 816 | "source": [ 817 | "* Delete Many " 818 | ] 819 | }, 820 | { 821 | "cell_type": "code", 822 | "execution_count": 61, 823 | "metadata": {}, 824 | "outputs": [ 825 | { 826 | "data": { 827 | "text/plain": [ 828 | "" 829 | ] 830 | }, 831 | "execution_count": 61, 832 | "metadata": {}, 833 | "output_type": "execute_result" 834 | } 835 | ], 836 | "source": [ 837 | "client['Languages']['language'].delete_many({})" 838 | ] 839 | }, 840 | { 841 | "cell_type": "markdown", 842 | "metadata": {}, 843 | "source": [ 844 | "# Level 3:\n", 845 | "if you really wnat to make a helper Library here is sample how i did using design pattern and metaclasses. i made a helper class on top of pymongo " 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": 62, 851 | "metadata": {}, 852 | "outputs": [ 853 | { 854 | "name": "stdout", 855 | "output_type": "stream", 856 | "text": [ 857 | "All Modules loaded \n" 858 | ] 859 | } 860 | ], 861 | "source": [ 862 | "__NAME__ = \"Soumil Nitin Shah \"\n", 863 | "__VERSION__ = \"0.0.1\"\n", 864 | "\n", 865 | "\n", 866 | "try:\n", 867 | " import os\n", 868 | " import pandas as pd\n", 869 | " import sys\n", 870 | " import io\n", 871 | " import pymongo\n", 872 | " import json\n", 873 | " from pymongo import MongoClient\n", 874 | " from bson.objectid import ObjectId\n", 875 | " print(\"All Modules loaded \")\n", 876 | "except Exception as e:\n", 877 | " print(\"Error : {} \".format(e))\n", 878 | "\n", 879 | "\n", 880 | "class Singleton(type):\n", 881 | "\n", 882 | " \"\"\"This would be a Singleton Design pattern \"\"\"\n", 883 | "\n", 884 | " _instance = {}\n", 885 | "\n", 886 | " def __call__(cls, *args, **kwargs):\n", 887 | " \"\"\"Creates one one instance of the class \"\"\"\n", 888 | " if cls not in cls._instance:\n", 889 | " cls._instance[cls] = super(Singleton, cls).__call__(*args, **kwargs)\n", 890 | " return cls._instance[cls]\n", 891 | "\n", 892 | "\n", 893 | "class Settings(metaclass=Singleton):\n", 894 | " def __init__(self, host=None, maxPoolSize=50, port=27010):\n", 895 | " self.host = host\n", 896 | " self.maxPoolSize = maxPoolSize\n", 897 | " self.port = port\n", 898 | "\n", 899 | "\n", 900 | "class Client(metaclass=Singleton):\n", 901 | "\n", 902 | " def __init__(self, _settings=None):\n", 903 | " self.settings = _settings\n", 904 | " self.mclient = MongoClient(host=self.settings.host,\n", 905 | " port=self.settings.port,\n", 906 | " maxPoolSize=self.settings.maxPoolSize)\n", 907 | "\n", 908 | "\n", 909 | "class MongoInsert(object):\n", 910 | "\n", 911 | " def __init__(self, _client=None, dbName=None, collectionName=None):\n", 912 | " self.client = _client\n", 913 | " self.dbName=dbName\n", 914 | " self.collectionName=collectionName\n", 915 | "\n", 916 | " def insert_one(self, record={}):\n", 917 | " \"\"\"\n", 918 | " Insert record in Mongo DB\n", 919 | " :param record: Json\n", 920 | " :return: Bool\n", 921 | " \"\"\"\n", 922 | " try:\n", 923 | " self.client.mclient[self.dbName][self.collectionName].insert_one(record)\n", 924 | " return True\n", 925 | " except Exception as e:\n", 926 | " print(\"Error : {} \".format(e))\n", 927 | " return False\n", 928 | "\n", 929 | " def insert_pandas_df(self,df=None):\n", 930 | " \"\"\"\n", 931 | " :param df: Pandas DF\n", 932 | " :return: Bool\n", 933 | " \"\"\"\n", 934 | "\n", 935 | " try:\n", 936 | " self.client.mclient[self.dbName][self.collectionName].insert_many(df.to_dict(), ordered=False)\n", 937 | " return True\n", 938 | " except Exception as e:\n", 939 | " return False\n", 940 | "\n", 941 | "\n", 942 | "class Mongoinformation(object):\n", 943 | "\n", 944 | " def __init__(self, _client=None):\n", 945 | " self.client = _client\n", 946 | "\n", 947 | " def getALllDatabase(self):\n", 948 | " \"\"\"\n", 949 | " Rreturn all Database Name in Mongo Db\n", 950 | " :return: List\n", 951 | " \"\"\"\n", 952 | " return self.client.mclient.list_database_names()\n", 953 | "\n", 954 | " def getAllCollections(self, DbName=None):\n", 955 | " \"\"\"\n", 956 | " :param DbName: Str\n", 957 | " :return: List\n", 958 | " \"\"\"\n", 959 | " if DbName is None:\n", 960 | " return []\n", 961 | " else:\n", 962 | " self.client.mclient[DbName].list_collection_names()\n", 963 | "\n", 964 | "\n", 965 | "class Mongoose(object):\n", 966 | "\n", 967 | " \"\"\"facade Class \"\"\"\n", 968 | "\n", 969 | " def __init__(self, host=None,port=27010, maxPoolSize=50,dbName=None, collectionName=None):\n", 970 | " self._settings =Settings(host=host, port=port, maxPoolSize=maxPoolSize) # Cretes a instance of settings class\n", 971 | " self.client = Client(_settings=self._settings) # creates a single instance of client object\n", 972 | " self.dbName=dbName\n", 973 | " self.collectionNam = collectionName\n", 974 | " self._insert = MongoInsert(_client=self.client, dbName=self.dbName,collectionName=self.collectionNam)\n", 975 | "\n", 976 | " def insert_one_record(self, record):\n", 977 | " return self._insert.insert_one(record=record)\n", 978 | "\n", 979 | "\n", 980 | "def main1():\n", 981 | " _helper = Mongoose(host=\"mongodb://root:rootpassword@localhost:27017\",dbName='person',collectionName='names')\n", 982 | " _record = {\"name\":\"Nitin shah\"}\n", 983 | " _helper.insert_one_record(record=_record)\n", 984 | "\n", 985 | "\n", 986 | "#\n", 987 | "# def main():\n", 988 | "#\n", 989 | "# url = \"mongodb://root:rootpassword@localhost:27017\"\n", 990 | "#\n", 991 | "# # Cretes a instance of settings class\n", 992 | "# _settings = Settings(host=url)\n", 993 | "#\n", 994 | "# # creates a single instance of client object\n", 995 | "# _client = Client(_settings=_settings)\n", 996 | "#\n", 997 | "# _record = {\"name\":\"Soumil shah\"}\n", 998 | "# dbName= \"person\"\n", 999 | "# collectionName = \"names\"\n", 1000 | "#\n", 1001 | "# _helper = MongoInsert(_client=_client, dbName=dbName,collectionName=collectionName)\n", 1002 | "# res = _helper.insert_one(record=_record)\n", 1003 | "# print(res)\n", 1004 | "\n", 1005 | "\n", 1006 | "#if __name__ == \"__main__\":\n", 1007 | "# main1()" 1008 | ] 1009 | }, 1010 | { 1011 | "cell_type": "code", 1012 | "execution_count": null, 1013 | "metadata": {}, 1014 | "outputs": [], 1015 | "source": [] 1016 | } 1017 | ], 1018 | "metadata": { 1019 | "kernelspec": { 1020 | "display_name": "Python 3", 1021 | "language": "python", 1022 | "name": "python3" 1023 | }, 1024 | "language_info": { 1025 | "codemirror_mode": { 1026 | "name": "ipython", 1027 | "version": 3 1028 | }, 1029 | "file_extension": ".py", 1030 | "mimetype": "text/x-python", 1031 | "name": "python", 1032 | "nbconvert_exporter": "python", 1033 | "pygments_lexer": "ipython3", 1034 | "version": "3.8.3" 1035 | } 1036 | }, 1037 | "nbformat": 4, 1038 | "nbformat_minor": 4 1039 | } 1040 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting-started-with-MongoDB-and-Python 2 | Learn about MongoDb using Pymongo and python 3 | 4 | ![Capture](https://user-images.githubusercontent.com/39345855/95038141-75bdc280-069b-11eb-939b-6f36212f35e8.JPG) 5 | 6 | 7 | * Part 1: Getting started with MongoDB using PyMongo | Python #1 Installing MongoDb using Docker Compose 8 | * Link https://www.youtube.com/watch?v=u6bOt3rp-Oo 9 | -------------------------------------------------------------------------------- /part4/queryGenerator.py: -------------------------------------------------------------------------------- 1 | try: 2 | import os 3 | import sys 4 | import io 5 | import json 6 | print("All Modules loaded ") 7 | except Exception as e: 8 | print("Error : {} ".format(e)) 9 | 10 | 11 | class QueryGenerator(object): 12 | def __init__(self): 13 | self.__base_query = { 14 | "$facet":{ 15 | 16 | } 17 | } 18 | 19 | def __str__(self): 20 | return "Mongo Db Query Generator" 21 | 22 | def add_aggregation(self, feildName=None, limit=5, columnName=None): 23 | """ 24 | addAggreadtion is withouth the Range 25 | 26 | X : 22 27 | Y: 77 28 | Above example shows this is how you will receive aggreation 29 | 30 | :param feildName: str 31 | :param limit: int 32 | :param columnName: str 33 | :return:str 34 | """ 35 | try: 36 | if feildName is None and columnName is None: 37 | return "Please Provide valid feild Name and columnName for Aggreation " 38 | else: 39 | _ = { 40 | "{}".format(feildName):[ 41 | {"$unwind": "${}".format(feildName) }, 42 | { "$sortByCount": "${}".format(feildName) }, 43 | { "$limit": limit} 44 | ] 45 | } 46 | self.__base_query['$facet'][columnName] = _.get(feildName) 47 | print("Query Added ") 48 | return "Query Added " 49 | except Exception as e: 50 | return "Error" 51 | 52 | def add_aggregation_range(self, columnName=None,feildName=None,limit=5 ): 53 | """ 54 | This will add Query for Range Based Aggreation in MongoDb 55 | 56 | This will add Query for $bucketAuto 57 | 58 | :param columnName: str 59 | :param feildName: str 60 | :param limit: int 61 | :return: str 62 | """ 63 | try: 64 | if columnName is None or feildName is None: 65 | return "Please Provide Valid columnName and feildName " 66 | else: 67 | _ = { 68 | "{}".format(columnName):[ 69 | { 70 | "$bucketAuto":{ 71 | "groupBy": "${}".format(feildName), 72 | "buckets": limit 73 | 74 | } 75 | 76 | } 77 | ] 78 | } 79 | self.__base_query['$facet'][columnName] = _.get(columnName) 80 | print("Query Added ") 81 | except Exception as e: 82 | pass 83 | 84 | @property 85 | def completeAggreation(self): 86 | return [self.__base_query] 87 | 88 | 89 | def main(): 90 | _helper = QueryGenerator() 91 | rule1 = _helper.add_aggregation(feildName='rating',columnName='Rating', limit=5) 92 | rule1 = _helper.add_aggregation_range(feildName='cast',columnName='Cast', limit=5) 93 | query = _helper.completeAggreation 94 | print(json.dumps(query , indent=3)) 95 | 96 | 97 | if __name__ == "__main__": 98 | main() --------------------------------------------------------------------------------