{"id":23093102,"url":"https://github.com/crispengari/python-and-mongodb","last_synced_at":"2026-04-13T01:05:11.714Z","repository":{"id":141094859,"uuid":"358010441","full_name":"CrispenGari/python-and-mongodb","owner":"CrispenGari","description":"💎 This is walk through on python + local mongodb and cloud mongodb using pymongo","archived":false,"fork":false,"pushed_at":"2022-09-12T14:11:32.000Z","size":397,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-03T18:52:03.558Z","etag":null,"topics":["database","dnspython","mongodb","mongodb-cloud","mongodb-database","pymong","python"],"latest_commit_sha":null,"homepage":"","language":"PowerShell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CrispenGari.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-14T18:51:26.000Z","updated_at":"2023-01-31T10:48:36.000Z","dependencies_parsed_at":"2024-05-31T05:02:58.412Z","dependency_job_id":null,"html_url":"https://github.com/CrispenGari/python-and-mongodb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/CrispenGari/python-and-mongodb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrispenGari%2Fpython-and-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrispenGari%2Fpython-and-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrispenGari%2Fpython-and-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrispenGari%2Fpython-and-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CrispenGari","download_url":"https://codeload.github.com/CrispenGari/python-and-mongodb/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrispenGari%2Fpython-and-mongodb/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264512322,"owners_count":23620315,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","dnspython","mongodb","mongodb-cloud","mongodb-database","pymong","python"],"created_at":"2024-12-16T21:46:28.850Z","updated_at":"2026-04-13T01:05:11.681Z","avatar_url":"https://github.com/CrispenGari.png","language":"PowerShell","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Python + MongoDb\n* this is a quick walk through python working with mongoD\n\n\u003cp align=\"center\"\u003e\n\u003cimg  src=\"https://img.shields.io/static/v1?label=language\u0026message=python\u0026color=green\"/\u003e\n\u003cimg src=\"https://img.shields.io/static/v1?label=package\u0026message=pymongo\u0026color=orange\"/\u003e\n\u003cimg src=\"https://img.shields.io/static/v1?label=database\u0026message=mongodb\u0026color=green\"/\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n\u003cimg width=\"60%\" align=\"center\" src=\"https://i.morioh.com/2020/02/24/a220d7ff8d12.jpg\" alt=\"py+mongo\"/\u003e\n\u003c/p\u003e\n\n### Setup\n1. First you need to download and install [MongoDB database](https://www.mongodb.com/try/download/community)\n2. Install pymongo\n   * to install pymongo run the command\n   ````shell\n   pip install pymongo\n   ````\n\n### Connecting to the Database\nTo connect to mongodb it is simple as doing the following:\n```\nimport pymongo\nclient = pymongo.MongoClient(host=\"localhost\", port=27017)\n```\nOR \n```\nimport pymongo\nclient = pymongo.MongoClient(\"mongodb://localhost:27017/\")\n```\n### Creating a database\n\u003e In mongoDB a database is only created if and only if it has contents in it:\n* Creating a new database in mongoDb is simple as doing the following\n\n```\nimport pymongo\nclient = pymongo.MongoClient(host=\"localhost\", port=27017)\ndb = client[\"shop\"]\n```\n### Listing Created Database\n* To list all created database using pymongo we do it as follows\n```\nprint(client.list_database_names())\n```\n\n### Creating a collection\n\u003e Just like creating a database a collection will be created if and only if it contains some contents in it.\n\n* Let's create a collection `customers` in our database.\n\n### Listing the collections that are in the database:\n* To lists all the collections that are in the database we do it as follows:\n```\nprint(db.collection_names())\n```\n\n### Inserting Documents in a collection\n* To insert documents in the collection we use the following methods:\n    1. `insert_one()`\n    2. `insert_many()`\n    \n#### 1. `insert_one()`\n* This method insert one document in the collection name example:\n```\nimport pymongo\nclient = pymongo.MongoClient(host=\"localhost\", port=27017)\ndb = client[\"shop\"]\ncustomers = db[\"customers\"]\ncustomer ={\n    \"name\":\"customer1\",\n}\ncursor = customers.insert_one(customer)\nprint(\"Inserted\", cursor.inserted_id)\n```\n#### 2. `insert_many()`\n* This method insert multiple documents in the collection:\n\n```\nimport pymongo\nclient = pymongo.MongoClient(host=\"localhost\", port=27017)\ndb = client[\"shop\"]\ncustomers = db[\"customers\"]\ncustomersInfo =[\n    {\"name\":\"customer3\"}, {\"name\":\"customer100\"}\n]\ncursor = customers.insert_many(customersInfo)\nprint(cursor.inserted_ids)\n```\n\n### Finding the docs\n* There are two methods in mongoDB that are used to query documents from a collection which are:\n    1. `find()`\n    2. `findOne()`\n\n\n#### 1. `findOne()`\n* used to get a single document that occurs first in a collection\n```\ncursor = customers.find_one({\"name\":\"customer3\"})\nprint(cursor)\n```\n#### 1. `find()`\n* get all the documents that meet a certain condition:\n```\ncursor = customers.find({\"name\":\"customer3\"})\nfor doc in cursor:\n    print(doc)\n```\n\u003e Listing all the docs in the collection:\n\n```\ncursor = customers.find({})\nfor doc in cursor:\n    print(doc)\n```\n\n### Sotting documents\n* In mongoDb the `sort()` method is used to sort the documents. For example let's sort our documents base on name:\n\n#### Ascending order\n```\ncursor = customers.find().sort(\"name\")\nfor doc in cursor:\n    print(doc)\nOR\ncursor = customers.find().sort(\"name\", 1)\nfor doc in cursor:\n    print(doc)\n```\n#### Descending Order\n```\ncursor = customers.find().sort(\"name\", -1)\nfor doc in cursor:\n    print(doc)\n```\n\n### Deleting documents\n* To delete documents we use the following methods:\n    1. `delete_one()`\n    2. `delete_many()`\n    \n#### 1. `delete_one()`\n* To delete one document we use the `delete_one()` method the following is an example on how to delete a document that matches a condition:\n```\ncursor = customers.delete_one({\"name\": \"customer100\"})\nprint(cursor.raw_result)\n```\n#### 1. `delete_many()`\n* To delete many document we use the `delete_many()` method the following is an example on how to delete a documents that matches a condition:\n```\ncursor = customers.delete_many({\"name\": \"customer3\"})\nprint(cursor.raw_result)\n```\n\n### Dropping a collection\n* Dropping a collection means deleting a collection from a database. Let's delete our collection `customers`\n```\ncursor = customers.drop()\nprint(db.collection_names()) # []\nprint(\"Collection deleted\")\n```\n### Updating Collection\n* To update a collection we use one of the methods which are:\n    1. `update_one()`\n    2. `update_many()`\n    \n    \n#### 1. `update_one()`\n* to update a single collection that matches a query we do it as follows:\n```\ncursor = customers.update_one({\"name\": \"Name1\"}, {\"$set\": {\"name\": \"Name100\"}})\nprint(cursor.raw_result, cursor.modified_count)\n```\n\n#### 2. `update_many()`\n* to update all collections that matches a query we do it as follows:\n```\ncursor = customers.update_one({\"name\": \"Name2\"}, {\"$set\": {\"name\": \"Name200\"}})\nprint(cursor.raw_result, cursor.modified_count)\n```\n\n### Limit the Result\nTo limit the results we use the `limit()` method which takes one parameter an integer which is the total number of documents that will be returned:\n\u003e Suppose we have the following documents in our collection and we want to get only first 5 customers we can do it as follows:\n \n```\n{'_id': 1, 'name': 'John', 'address': 'Highway37'}\n{'_id': 2, 'name': 'Peter', 'address': 'Lowstreet 27'}\n{'_id': 3, 'name': 'Amy', 'address': 'Apple st 652'}\n{'_id': 4, 'name': 'Hannah', 'address': 'Mountain 21'}\n{'_id': 5, 'name': 'Michael', 'address': 'Valley 345'}\n{'_id': 6, 'name': 'Sandy', 'address': 'Ocean blvd 2'}\n{'_id': 7, 'name': 'Betty', 'address': 'Green Grass 1'}\n{'_id': 8, 'name': 'Richard', 'address': 'Sky st 331'}\n{'_id': 9, 'name': 'Susan', 'address': 'One way 98'}\n{'_id': 10, 'name': 'Vicky', 'address': 'Yellow Garden 2'}\n{'_id': 11, 'name': 'Ben', 'address': 'Park Lane 38'}\n{'_id': 12, 'name': 'William', 'address': 'Central st 954'}\n{'_id': 13, 'name': 'Chuck', 'address': 'Main Road 989'}\n{'_id': 14, 'name': 'Viola', 'address': 'Sideway 1633'}\n```\n#### `code`:\n```\ncursor = customers.find().limit(5)\nfor doc in cursor:\n    print(doc)\n```\n\u003e That's the basics about python + mongodb using `pymongo`\n\n## Cloud MongoDB\n* [Here](https://github.com/CrispenGari/python-and-mongodb/tree/main/cloudmongodb)\n### Credits:\n* [w3schools](https://www.w3schools.com/python/python_reference.asp)\n* [pymongo](https://pymongo.readthedocs.io/en/stable/)\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrispengari%2Fpython-and-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrispengari%2Fpython-and-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrispengari%2Fpython-and-mongodb/lists"}