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\nif 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\n2. Import MongoClient and create a connection<\/h2>\n\n\n\nfrom pymongo import MongoClient\n\nclient = MongoClient()<\/code><\/pre>\n\n\n\nyou 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\nYou 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\nMongoDB URI with username and password<\/h2>\n\n\n\nURI = 'mongodb:\/\/USER:PASSWORD@HOST\/DATABASE'<\/code><\/pre>\n\n\n\nIf 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\nfrom 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\n3. Close Connection<\/h2>\n\n\n\nclient.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":"\n
Connect to MongoDB from Python - Jennifer Kwentoh<\/title>\n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n