Connect to MongoDB from Python
To connect to a MongoDB instance, pymongo provides pymongo.MongoClient() class. You can pass the web URL of the MongoDB instance as shown in the following program.
Make sure that Mongo Daemon is up and running at the URL you specified.
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/")
If the above pymongo example program runs successfully, then a connection is said to made to the mongodb instance.
If you provide a wrong mongodb instance URL, or if the mongodb instance is not up, you will receive pymongo.errors.ServerSelectionTimeoutError.
In this tutorial of Python Examples, we learned how to connect to MongoDB from a Python program using pymongo library.