{"id":19393170,"url":"https://github.com/ruanbekker/mongodb-with-python-tutorial","last_synced_at":"2025-07-15T16:33:20.876Z","repository":{"id":139706994,"uuid":"180454195","full_name":"ruanbekker/mongodb-with-python-tutorial","owner":"ruanbekker","description":"MongoDB with Pymongo Tutorial","archived":false,"fork":false,"pushed_at":"2024-04-19T08:21:02.000Z","size":45,"stargazers_count":10,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-12T01:17:19.807Z","etag":null,"topics":["mongodb","mongodb-tutorial","nosql","nosql-database","pymongo","python"],"latest_commit_sha":null,"homepage":null,"language":null,"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/ruanbekker.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":"2019-04-09T21:46:02.000Z","updated_at":"2024-04-05T10:34:52.000Z","dependencies_parsed_at":"2024-04-19T09:31:43.849Z","dependency_job_id":null,"html_url":"https://github.com/ruanbekker/mongodb-with-python-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ruanbekker/mongodb-with-python-tutorial","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruanbekker%2Fmongodb-with-python-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruanbekker%2Fmongodb-with-python-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruanbekker%2Fmongodb-with-python-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruanbekker%2Fmongodb-with-python-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruanbekker","download_url":"https://codeload.github.com/ruanbekker/mongodb-with-python-tutorial/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruanbekker%2Fmongodb-with-python-tutorial/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265445320,"owners_count":23766448,"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":["mongodb","mongodb-tutorial","nosql","nosql-database","pymongo","python"],"created_at":"2024-11-10T10:32:33.265Z","updated_at":"2025-07-15T16:33:20.826Z","avatar_url":"https://github.com/ruanbekker.png","language":null,"readme":"# mongodb-with-python-tutorial\nMongoDB with Pymongo Tutorial\n\n### MongoDB Server\n\nRun a mongodb server with docker:\n\n```\n$ docker run --rm -itd --name mongodb -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=password -p 27017:27017 mongo:4.4\n```\n\n### Python Package\n\nInstall the python package to interact with mongodb using python:\n\n```\n$ pip install pymongo\n```\n\n### Making a Connection\n\nMaking a connection without authentication:\n\n```\n\u003e\u003e\u003e from pymongo import MongoClient\n\u003e\u003e\u003e uri = 'mongodb://localhost:27017/'\n\u003e\u003e\u003e client = MongoClient(uri)\n\u003e\u003e\u003e client.list_database_names()\n['admin', 'config', 'local']\n```\n\nMaking a connection with authentication:\n\n```\n\u003e\u003e\u003e from pymongo import MongoClient\n\u003e\u003e\u003e uri = 'mongodb://root:password@localhost:27017/admin?authSource=admin\u0026authMechanism=SCRAM-SHA-1'\n\u003e\u003e\u003e client = MongoClient(uri)\n\u003e\u003e\u003e client.list_database_names()\n['admin', 'config', 'local']\n```\n\n### Listing Databases\n\n\n```\n\u003e\u003e\u003e client.list_database_names()\n['admin', 'config', 'local']\n```\n\n### Listing Collections\n\n```\n\u003e\u003e\u003e db = client.config\n\u003e\u003e\u003e db.list_collection_names()\n['system.sessions']\n```\n\n### Write One Document \n\nCreate a database `store_db`, use the `transactions` collection and write a document to it.\n\n```\n\u003e\u003e\u003e db = client.store_db\n\u003e\u003e\u003e transactions = db.transactions\n\u003e\u003e\u003e doc_data = {\n    'store_name': 'sportsmans', \n    'branch_name': 'tygervalley', \n    'account_id': 'sns_03821023', \n    'total_costs': 109.20, \n    'products_purchased': ['cricket bat', 'cricket ball', 'sports hat'], \n    'purchase_method': \n    'credit card'\n}\n\u003e\u003e\u003e response = transactions.insert_one(doc_data)\n\u003e\u003e\u003e response.inserted_id\nObjectId('5cad16a5a5f3826f6f046d74')\n```\n\nWe can verify that the collection is present:\n\n```\n\u003e\u003e\u003e db.list_collection_names()\n['transactions']\n```\n\n### Write Many Documents\n\nWe can batch up our writes:\n\n```\n\u003e\u003e\u003e transaction_1 = {\n    'store_name': 'sportsmans', 'branch_name': 'tygervalley', \n    'account_id': 'sns_09121024', 'total_costs': 129.84, \n    'products_purchased': ['sportsdrink', 'sunglasses', 'sports illustrated'], \n    'purchase_method': 'credit card'\n}\n\u003e\u003e\u003e transaction_2 = {\n    'store_name': 'burger king', 'branch_name': \n    'somerset west', 'account_id': 'bk_29151823', \n    'total_costs': 89.99, 'products_purchased': ['cheese burger', 'pepsi'], \n    'purchase_method': 'cash'\n}\n\u003e\u003e\u003e transaction_3 = {\n    'store_name': 'game', 'branch_name': 'bellvile', 'account_id': 'gm_49121229', \n    'total_costs': 499.99, 'products_purchased': ['ps4 remote'], \n    'purchase_method': 'cash'\n}\n\u003e\u003e\u003e response = transactions.insert_many([transaction_1, transaction_2, transaction_3])\n\u003e\u003e\u003e response.inserted_ids\n[ObjectId('5cad18d4a5f3826f6f046d75'), ObjectId('5cad18d4a5f3826f6f046d76'), ObjectId('5cad18d4a5f3826f6f046d77')]\n```\n\n### Find One Document:\n\n```\n\u003e\u003e\u003e transactions.find_one({'account_id': 'gm_49121229'})\n{u'account_id': u'gm_49121229', u'store_name': u'game', u'purchase_method': u'cash', u'branch_name': u'bellvile', u'products_purchased': [u'ps4 remote'], u'_id': ObjectId('5cad18d4a5f3826f6f046d77'), u'total_costs': 499.99}\n```\n\n### Find Many Documents:\n\n```\n\u003e\u003e\u003e response = transactions.find({'purchase_method': 'cash'})\n\u003e\u003e\u003e [doc for doc in response]\n[{u'account_id': u'bk_29151823', u'store_name': u'burger king', u'purchase_method': u'cash', u'branch_name': u'somerset west', u'products_purchased': [u'cheese burger', u'pepsi'], u'_id': ObjectId('5cad18d4a5f3826f6f046d76'), u'total_costs': 89.99}, {u'account_id': u'gm_49121229', u'store_name': u'game', u'purchase_method': u'cash', u'branch_name': u'bellvile', u'products_purchased': [u'ps4 remote'], u'_id': ObjectId('5cad18d4a5f3826f6f046d77'), u'total_costs': 499.99}]\n```\n\nOr filtering down the results to only the account id:\n\n```\n\u003e\u003e\u003e response = transactions.find({'purchase_method': 'cash'})\n\u003e\u003e\u003e [doc['account_id'] for doc in response]\n[u'bk_29151823', u'gm_49121229']\n```\n\nMongoDB also has a count method:\n\n```\n\u003e\u003e\u003e transactions.count_documents({'purchase_method': 'cash'})\n2\n```\n\nQuery with the AND condition. SQL equivalent: `where branch_name = 'tygervalley' AND account_id = 'sns_03821023'`\n\n```\n\u003e\u003e\u003e response = transactions.find({ '$and': [{'branch_name': 'tygervalley'},{'account_id': 'sns_03821023'}]})\n\u003e\u003e\u003e [v for v in response]\n[{u'account_id': u'sns_03821023', u'store_name': u'sportsmans', u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'products_purchased': [u'cricket bat', u'cricket ball', u'sports hat'], u'_id': ObjectId('5cb18881df585e003c976d5d'), u'total_costs': 109.2}]\n```\n\nQuery with the OR condition. SQL equivalent: `where branch_name = 'tygervalley' OR account_id = 'sns_03821023'`:\n\n```\n\u003e\u003e\u003e response = transactions.find({ '$or': [{'branch_name': 'tygervalley'},{'account_id': 'sns_03821023'}]})\n\u003e\u003e\u003e [v for v in response]\n[{u'account_id': u'sns_03821023', u'store_name': u'sportsmans', u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'products_purchased': [u'cricket bat', u'cricket ball', u'sports hat'], u'_id': ObjectId('5cb18881df585e003c976d5d'), u'total_costs': 109.2}, {u'account_id': u'sns_09121024', u'store_name': u'sportsmans', u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'products_purchased': [u'sportsdrink', u'sunglasses', u'sports illustrated'], u'_id': ObjectId('5cb188a3df585e003c976d5e'), u'total_costs': 129.84}]\n```\n\nCombining it:\n\n```\n\u003e\u003e\u003e response = transactions.find({ 'total_costs': {'$gt': 120}, '$or': [{'branch_name': 'tygervalley'},{'account_id': 'sns_03821023'}]})\n\u003e\u003e\u003e [v for v in response]\n[{u'account_id': u'sns_09121024', u'store_name': u'sportsmans', u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'products_purchased': [u'sportsdrink', u'sunglasses', u'sports illustrated'], u'_id': ObjectId('5cb188a3df585e003c976d5e'), u'total_costs': 129.84}]\n```\n\nOther condition operations include:\n\n```\nlt  - Less Than\nlte - Less Than Equals\ngt  - Greater Than\ngte - Greater Than Equals\nne  - Not Equals\n```\n\n## Range Queries:\n\n```\n\u003e\u003e\u003e import datetime\n\u003e\u003e\u003e new_posts = [{\"author\": \"Mike\",\"text\": \"Another post!\",\"tags\": [\"bulk\", \"insert\"],\"date\": datetime.datetime(2009, 11, 12, 11, 14)},{\"author\": \"Eliot\",\"title\": \"MongoDB is fun\",\"text\": \"and pretty easy too!\",\"date\": datetime.datetime(2009, 11, 10, 10, 45)}]\n\u003e\u003e\u003e db.posts.insert(new_posts)\n[ObjectId('5cb439c0f90e2e002a164d15'), ObjectId('5cb439c0f90e2e002a164d16')]\n\n\u003e\u003e\u003e for post in db.posts.find():\n...     post\n...\n{u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!', u'_id': ObjectId('5cb439c0f90e2e002a164d15'), u'author': u'Mike', u'tags': [u'bulk', u'insert']}\n{u'date': datetime.datetime(2009, 11, 10, 10, 45), u'text': u'and pretty easy too!', u'_id': ObjectId('5cb439c0f90e2e002a164d16'), u'author': u'Eliot', u'title': u'MongoDB is fun'}\n\n\u003e\u003e\u003e d = datetime.datetime(2009, 11, 11, 12)\n\u003e\u003e\u003e for post in db.posts.find({\"date\": {\"$lt\": d}}).sort(\"author\"):\n...     post\n...\n{u'date': datetime.datetime(2009, 11, 10, 10, 45), u'text': u'and pretty easy too!', u'_id': ObjectId('5cb439c0f90e2e002a164d16'), u'author': u'Eliot', u'title': u'MongoDB is fun'}\n```\n\n## Updates\n\nLet's say we want to change a transactions payment method from credit card to account:\n\n```\n\u003e\u003e\u003e transactions.find_one({'account_id':'sns_03821023'})\n{u'account_id': u'sns_03821023', u'store_name': u'sportsmans', u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'products_purchased': [u'cricket bat', u'cricket ball', u'sports hat'], u'_id': ObjectId('5cb18881df585e003c976d5d'), u'total_costs': 109.2}```\n```\n\nUpdate the purchase_method to account:\n\n```\n\u003e\u003e\u003e transactions.update( {'account_id': 'sns_03821023'}, {'$set': {'purchase_method': 'account'}})\n{'updatedExisting': True, u'nModified': 1, u'ok': 1.0, u'n': 1}\n```\n\nVerify:\n\n```\n\u003e\u003e\u003e transactions.find_one({'account_id':'sns_03821023'})\n{u'account_id': u'sns_03821023', u'store_name': u'sportsmans', u'purchase_method': u'account', u'branch_name': u'tygervalley', u'products_purchased': [u'cricket bat', u'cricket ball', u'sports hat'], u'_id': ObjectId('5cb18881df585e003c976d5d'), u'total_costs': 109.2}\n```\n\nThis examples is only intended for a single document and mongodb only updates one document at a time. To update more than one at a time:\n\n```\ntransactions.update( {'k': 'v'}, {'$set': {'k': 'new_v'}},{multi:true})\n```\n\n\n### Filters\n\nFind all the documents with purchase price \u003e 120:\n\n```\n\u003e\u003e\u003e response = transactions.find({'total_costs': {'$gt': 120}})\n\u003e\u003e\u003e [doc for doc in response]\n[{u'account_id': u'sns_09121024', u'store_name': u'sportsmans', u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'products_purchased': [u'sportsdrink', u'sunglasses', u'sports illustrated'], u'_id': ObjectId('5cad18d4a5f3826f6f046d75'), u'total_costs': 129.84}, {u'account_id': u'gm_49121229', u'store_name': u'game', u'purchase_method': u'cash', u'branch_name': u'bellvile', u'products_purchased': [u'ps4 remote'], u'_id': ObjectId('5cad18d4a5f3826f6f046d77'), u'total_costs': 499.99}]\n```\n\n### Projections\n\nSelect specific fields from the returned response:\n\n```\n\u003e\u003e\u003e response = transactions.find({}, {'branch_name': 'tygervalley', 'purchase_method': 'credit card'})\n\u003e\u003e\u003e [doc for doc in response]\n[{u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'_id': ObjectId('5cad16a5a5f3826f6f046d74')}, {u'purchase_method': u'credit card', u'branch_name': u'tygervalley', u'_id': ObjectId('5cad18d4a5f3826f6f046d75')}, {u'purchase_method': u'cash', u'branch_name': u'somerset west', u'_id': ObjectId('5cad18d4a5f3826f6f046d76')}, {u'purchase_method': u'cash', u'branch_name': u'bellvile', u'_id': ObjectId('5cad18d4a5f3826f6f046d77')}]\n```\n\n### Sorting Documents\n\nSorting Documents in Descending Order:\n\n```\n\u003e\u003e\u003e from pymongo import MongoClient, DESCENDING\n\u003e\u003e\u003e response = transactions.find().sort('total_costs', DESCENDING)\n\u003e\u003e\u003e ['Products: {}, Price: {}'.format(doc['products_purchased'], doc['total_costs']) for doc in response]\n[\"Products: [u'ps4 remote'], Price: 499.99\", \"Products: [u'sportsdrink', u'sunglasses', u'sports illustrated'], Price: 129.84\", \"Products: [u'cricket bat', u'cricket ball', u'sports hat'], Price: 109.2\", \"Products: [u'cheese burger', u'pepsi'], Price: 89.99\"]\n```\n\n### Aggregations\n\n```\n\u003e\u003e\u003e agr = [{'$group': {'_id': 1, 'all': { '$sum': '$total_costs' }}}]\n\u003e\u003e [a for a in transactions.aggregate(agr)]\n[{u'all': 829.02, u'_id': 1}]\n```\n\nor:\n\n```\n\u003e\u003e\u003e agr = [{'$group': {'_id': 1, 'all': { '$sum': '$total_costs' }}}]\n\u003e\u003e\u003e val = list(transactions.aggregate(agr))\n\u003e\u003e\u003e val\n[{u'all': 829.02, u'_id': 1}]\n```\n\nSelect fields to aggregate, eg. aggregate the costs for selected stores:\n\n```\n\u003e\u003e\u003e agr = [{ '$match': {'$or': [ { 'store_name': 'sportsmans' }, { 'store_name': 'game' }] }}, { '$group': {'_id': 1, 'sum2stores': { '$sum': '$total_costs' } }}]\n\u003e\u003e\u003e [a for a in transactions.aggregate(agr)]\n[{u'_id': 1, u'sum2stores': 739.03}]\n```\n\n### Limit Data Output\n\n```\n\u003e\u003e\u003e response = transactions.find()\n\u003e\u003e\u003e [a['account_id'] for a in response]\n[u'sns_03821023', u'sns_09121024', u'bk_29151823', u'gm_49121229']\n\n\u003e\u003e\u003e response = transactions.find().limit(2)\n\u003e\u003e\u003e [a['account_id'] for a in response]\n[u'sns_03821023', u'sns_09121024']\n\n\u003e\u003e\u003e response = transactions.find().skip(1).limit(3)\n\u003e\u003e\u003e [a['account_id'] for a in response]\n[u'sns_09121024', u'bk_29151823', u'gm_49121229']\n\n\u003e\u003e\u003e response = transactions.find().skip(1).limit(2)\n\u003e\u003e\u003e [a['account_id'] for a in response]\n[u'sns_09121024', u'bk_29151823']\n\n\u003e\u003e\u003e response = transactions.find().skip(3).limit(1)\n\u003e\u003e\u003e [a['account_id'] for a in response]\n[u'gm_49121229']\n```\n\n## Indexes\n\nCreate index:\n\n```\n\u003e\u003e\u003e from pymongo import TEXT\n\u003e\u003e\u003e transactions.create_index([(\"store_name\", TEXT)], name='store_index', default_language='english')\n'store_index'\n```\n\n## Delete Documents:\n\nDelete selected documents:\n\n```\n\u003e\u003e\u003e transactions.remove({'account_id':'sns_03821029'})\n{u'ok': 1.0, u'n': 2}\n```\n\nDelete all documents:\n\n```\n\u003e\u003e\u003e transactions.remove()\n```\n\n### Drop Collections\n\n```\n\u003e\u003e\u003e transactions.drop()\n\u003e\u003e\u003e db.collection_names()\n[]\n```\n\n### Drop Databases\n\n```\n\u003e\u003e\u003e c.drop_database('store_db')\n```\n\n### MongoEngine - ORM\n\n```\n\u003e\u003e\u003e from mongoengine import *\n\u003e\u003e\u003e connect('project1', host=\"mongodb://user:pass@mongodb.domain.com:27017/random_api?authSource=admin\u0026authMechanism=SCRAM-SHA-1\")\nMongoClient(host=['mongodb.domain.com:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary(), authsource='admin', authmechanism='SCRAM-SHA-1')\n```\n\n```\n\u003e\u003e\u003e class Student(Document):\n    name = StringField(required=True, max_length=200)\n    city = StringField(required=True, max_length=200)\n    can_code = BooleanField(required=True)\n```\n\nCreate a student:\n\n```\n\u003e\u003e\u003e doc_1 = Student(name='Josh', city='Cape Town', can_code=True)\n\u003e\u003e\u003e doc_1.save()\n\u003cStudent: Student object\u003e\n\u003e\u003e\u003e doc_1.id\nObjectId('5cad27dea5f38276a40f43db')\n\u003e\u003e\u003e doc_1.name\nu'Josh'\n\u003e\u003e\u003e doc_1.city\nu'Cape Town'\n\u003e\u003e\u003e doc_1.can_code\nTrue\n```\n\nTest out validation:\n\n```\n\u003e\u003e\u003e doc_2 = Student(name='Max', city='Cape Town')\n\u003e\u003e\u003e doc_2.save()\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\n  File \"/usr/local/lib/python2.7/site-packages/mongoengine/document.py\", line 369, in save\n    self.validate(clean=clean)\n  File \"/usr/local/lib/python2.7/site-packages/mongoengine/base/document.py\", line 392, in validate\n    raise ValidationError(message, errors=errors)\nmongoengine.errors.ValidationError: ValidationError (Student:None) (Field is required: ['can_code'])\n```\n\nUpdate a user's city:\n\n```\n\u003e\u003e\u003e doc_2 = Student(name='Max', city='Cape Town', can_code=False)\n\u003e\u003e\u003e doc_2.save()\n\u003cStudent: Student object\u003e\n\u003e\u003e\u003e doc_2.id\nObjectId('5cad2835a5f38276a40f43dc')\n\u003e\u003e\u003e doc_2.city\nu'Cape Town'\n\u003e\u003e\u003e doc_2.city = 'Johannesburg'\n\u003e\u003e\u003e doc_2.save()\n\u003cStudent: Student object\u003e\n\u003e\u003e\u003e doc_2.city\n'Johannesburg'\n```\n\n## Datasets:\n- https://raw.githubusercontent.com/steveren/docs-assets/charts-tutorial/movieDetails.json\n\n\n## Resources\n\n- https://itnext.io/indexing-and-mongodb-query-performance-a8a6a64c4308\n- https://realpython.com/introduction-to-mongodb-and-python/\n- https://docs.mongodb.com/charts/master/tutorial/movie-details/prereqs-and-import-data/\n- https://www.tutorialspoint.com/mongodb/mongodb_relationships.htm\n- http://zetcode.com/python/pymongo/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruanbekker%2Fmongodb-with-python-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruanbekker%2Fmongodb-with-python-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruanbekker%2Fmongodb-with-python-tutorial/lists"}