{"id":1808,"date":"2022-12-05T13:16:07","date_gmt":"2022-12-05T13:16:07","guid":{"rendered":"https:\/\/jenniferkwentoh.com\/?p=1808"},"modified":"2022-12-05T20:53:34","modified_gmt":"2022-12-05T20:53:34","slug":"connect-to-mongodb-from-python","status":"publish","type":"post","link":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/","title":{"rendered":"Connect to MongoDB from Python"},"content":{"rendered":"\n

Connect to MongoDB from Python 3<\/h2>\n\n\n\n

Connect to MongoDB from Python<\/strong> code<\/strong> – Follow these steps to connect to your Mongo database from your python code. <\/p>\n\n\n\n

1. Install PyMongo <\/h2>\n\n\n\n

PyMongo<\/a> supports CPython 3.7+<\/code> and PyPy3.7+<\/code><\/p>\n\n\n\n

PyMongo official documentation recommends using pip<\/a><\/p>\n\n\n\n

$ python3 -m pip install pymongo<\/code><\/pre>\n\n\n\n
Collecting pymongo\n  Downloading pymongo-4.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (501 kB)\n     |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 501 kB 346 kB\/s\nRequirement already satisfied: dnspython<3.0.0,>=1.16.0 in \/home\/jennifer\/anaconda3\/lib\/python3.8\/site-packages (from pymongo) (2.2.1)\nInstalling collected packages: pymongo\nSuccessfully installed pymongo-4.3.3<\/code><\/pre>\n\n\n\n

if you already have pymongo and want to upgrade, run the line below<\/p>\n\n\n\n

$ python3 -m pip install --upgrade pymongo<\/code><\/pre>\n\n\n\n

2. Import MongoClient and create a connection<\/h2>\n\n\n\n
from pymongo import MongoClient\n\nclient = MongoClient()<\/code><\/pre>\n\n\n\n

you can specify the host address and port<\/p>\n\n\n\n

from pymongo import MongoClient\nhost = 'localhost'\nport = 27017\nclient = MongoClient(host, port)<\/code><\/pre>\n\n\n\n

You can also use mongodb URI format<\/p>\n\n\n\n

from pymongo import MongoClient\nURI = 'mongodb:\/\/localhost:27017'\nclient = MongoClient(URI)<\/code><\/pre>\n\n\n\n

MongoDB URI with username and password<\/h2>\n\n\n\n
URI = 'mongodb:\/\/USER:PASSWORD@HOST\/DATABASE'<\/code><\/pre>\n\n\n\n

If the username and password have special characters, we can escape them according to RFC 3986<\/code> using urllib.parse.quote_plus<\/code><\/p>\n\n\n\n

from pymongo import MongoClient\nfrom urllib.parse import quote_plus as urlquote\n\nuser = 'mongouser'\npassword = 'mongo@strong!pasword'\nhost = 'localhost'\nport = 27017\nURI = f'mongodb:\/\/{urlquote(user)}:{urlquote(password)}@{host}:{port}'\nclient = MongoClient(URI)<\/code><\/pre>\n\n\n\n

3. Close Connection<\/h2>\n\n\n\n
client.close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

Connect to MongoDB from Python 3 Connect to MongoDB from Python code – Follow these steps to connect to your Mongo database from your python code. 1. Install PyMongo PyMongo supports CPython 3.7+ and PyPy3.7+ PyMongo official documentation recommends using pip if you already have pymongo and want to upgrade, run the line below 2. […]<\/p>\n","protected":false},"author":1,"featured_media":1817,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"tdm_status":"","tdm_grid_status":"","footnotes":""},"categories":[2382,2387,2384],"tags":[2389,2388],"yoast_head":"\nConnect to MongoDB from Python - Jennifer Kwentoh<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Connect to MongoDB from Python - Jennifer Kwentoh\" \/>\n<meta property=\"og:description\" content=\"Connect to MongoDB from Python 3 Connect to MongoDB from Python code – Follow these steps to connect to your Mongo database from your python code. 1. Install PyMongo PyMongo supports CPython 3.7+ and PyPy3.7+ PyMongo official documentation recommends using pip if you already have pymongo and want to upgrade, run the line below 2. […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Jennifer Kwentoh\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-05T13:16:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-05T20:53:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1440\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Jennifer\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/jennifazor\" \/>\n<meta name=\"twitter:site\" content=\"@jennifazor\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jennifer\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/\"},\"author\":{\"name\":\"Jennifer\",\"@id\":\"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/a3d535307b7baa28d006f8421afa03c9\"},\"headline\":\"Connect to MongoDB from Python\",\"datePublished\":\"2022-12-05T13:16:07+00:00\",\"dateModified\":\"2022-12-05T20:53:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/\"},\"wordCount\":98,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/a3d535307b7baa28d006f8421afa03c9\"},\"image\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp\",\"keywords\":[\"database\",\"MongoDB\"],\"articleSection\":[\"Data Engineering\",\"Database\",\"Everything Data\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/\",\"url\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/\",\"name\":\"Connect to MongoDB from Python - Jennifer Kwentoh\",\"isPartOf\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp\",\"datePublished\":\"2022-12-05T13:16:07+00:00\",\"dateModified\":\"2022-12-05T20:53:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#primaryimage\",\"url\":\"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp\",\"contentUrl\":\"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp\",\"width\":2560,\"height\":1440,\"caption\":\"connect to mongodb from python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jenniferkwentoh.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Connect to MongoDB from Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jenniferkwentoh.com\/#website\",\"url\":\"https:\/\/jenniferkwentoh.com\/\",\"name\":\"Jennifer Kwentoh\",\"description\":\"superhero building intelligent agents..\",\"publisher\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/a3d535307b7baa28d006f8421afa03c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jenniferkwentoh.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/a3d535307b7baa28d006f8421afa03c9\",\"name\":\"Jennifer\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/i1.wp.com\/jenniferkwentoh.com\/wp-content\/uploads\/2020\/02\/J.kwentoh-Icon-1.png?fit=325%2C377&ssl=1\",\"contentUrl\":\"https:\/\/i1.wp.com\/jenniferkwentoh.com\/wp-content\/uploads\/2020\/02\/J.kwentoh-Icon-1.png?fit=325%2C377&ssl=1\",\"width\":325,\"height\":377,\"caption\":\"Jennifer\"},\"logo\":{\"@id\":\"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/image\/\"},\"description\":\"My name is Jennifer Chiazor Kwentoh, and I am a Machine Learning Engineer. I build production-ready machine learning systems. When I'm not burning out my GPUs, I spend time painting beautiful portraits.\",\"sameAs\":[\"https:\/\/jenniferkwentoh.com\/about\",\"https:\/\/linkedin.com\/in\/jennifer-c-kwentoh\",\"https:\/\/twitter.com\/https:\/\/twitter.com\/jennifazor\",\"https:\/\/www.youtube.com\/channel\/UCXpUz9dulsRB_Tls0DqjS-w\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Connect to MongoDB from Python - Jennifer Kwentoh","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/","og_locale":"en_US","og_type":"article","og_title":"Connect to MongoDB from Python - Jennifer Kwentoh","og_description":"Connect to MongoDB from Python 3 Connect to MongoDB from Python code – Follow these steps to connect to your Mongo database from your python code. 1. Install PyMongo PyMongo supports CPython 3.7+ and PyPy3.7+ PyMongo official documentation recommends using pip if you already have pymongo and want to upgrade, run the line below 2. […]","og_url":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/","og_site_name":"Jennifer Kwentoh","article_published_time":"2022-12-05T13:16:07+00:00","article_modified_time":"2022-12-05T20:53:34+00:00","og_image":[{"width":2560,"height":1440,"url":"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp","type":"image\/webp"}],"author":"Jennifer","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/jennifazor","twitter_site":"@jennifazor","twitter_misc":{"Written by":"Jennifer","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#article","isPartOf":{"@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/"},"author":{"name":"Jennifer","@id":"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/a3d535307b7baa28d006f8421afa03c9"},"headline":"Connect to MongoDB from Python","datePublished":"2022-12-05T13:16:07+00:00","dateModified":"2022-12-05T20:53:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/"},"wordCount":98,"commentCount":0,"publisher":{"@id":"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/a3d535307b7baa28d006f8421afa03c9"},"image":{"@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#primaryimage"},"thumbnailUrl":"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp","keywords":["database","MongoDB"],"articleSection":["Data Engineering","Database","Everything Data"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/","url":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/","name":"Connect to MongoDB from Python - Jennifer Kwentoh","isPartOf":{"@id":"https:\/\/jenniferkwentoh.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#primaryimage"},"image":{"@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#primaryimage"},"thumbnailUrl":"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp","datePublished":"2022-12-05T13:16:07+00:00","dateModified":"2022-12-05T20:53:34+00:00","breadcrumb":{"@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#primaryimage","url":"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp","contentUrl":"https:\/\/jenniferkwentoh.com\/wp-content\/uploads\/2022\/12\/connect-to-mongodb-from-python.webp","width":2560,"height":1440,"caption":"connect to mongodb from python"},{"@type":"BreadcrumbList","@id":"https:\/\/jenniferkwentoh.com\/connect-to-mongodb-from-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jenniferkwentoh.com\/"},{"@type":"ListItem","position":2,"name":"Connect to MongoDB from Python"}]},{"@type":"WebSite","@id":"https:\/\/jenniferkwentoh.com\/#website","url":"https:\/\/jenniferkwentoh.com\/","name":"Jennifer Kwentoh","description":"superhero building intelligent agents..","publisher":{"@id":"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/a3d535307b7baa28d006f8421afa03c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jenniferkwentoh.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/a3d535307b7baa28d006f8421afa03c9","name":"Jennifer","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/image\/","url":"https:\/\/i1.wp.com\/jenniferkwentoh.com\/wp-content\/uploads\/2020\/02\/J.kwentoh-Icon-1.png?fit=325%2C377&ssl=1","contentUrl":"https:\/\/i1.wp.com\/jenniferkwentoh.com\/wp-content\/uploads\/2020\/02\/J.kwentoh-Icon-1.png?fit=325%2C377&ssl=1","width":325,"height":377,"caption":"Jennifer"},"logo":{"@id":"https:\/\/jenniferkwentoh.com\/#\/schema\/person\/image\/"},"description":"My name is Jennifer Chiazor Kwentoh, and I am a Machine Learning Engineer. I build production-ready machine learning systems. When I'm not burning out my GPUs, I spend time painting beautiful portraits.","sameAs":["https:\/\/jenniferkwentoh.com\/about","https:\/\/linkedin.com\/in\/jennifer-c-kwentoh","https:\/\/twitter.com\/https:\/\/twitter.com\/jennifazor","https:\/\/www.youtube.com\/channel\/UCXpUz9dulsRB_Tls0DqjS-w"]}]}},"_links":{"self":[{"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/posts\/1808"}],"collection":[{"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/comments?post=1808"}],"version-history":[{"count":11,"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/posts\/1808\/revisions"}],"predecessor-version":[{"id":1822,"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/posts\/1808\/revisions\/1822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/media\/1817"}],"wp:attachment":[{"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/media?parent=1808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/categories?post=1808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jenniferkwentoh.com\/wp-json\/wp\/v2\/tags?post=1808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}