Connect to MongoDB
On this page
Overview
This page contains code examples that show how to connect your Python application to MongoDB with various settings.
Tip
To learn more about the connection options on this page, see the link provided in each section.
To use a connection example from this page, copy the code example into the
sample application or your own application.
Be sure to replace all placeholders in the code examples, such as <hostname>
, with
the relevant values for your MongoDB deployment.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have PyMongo installed.
Copy the following code and paste it into a new
.py
file.Copy a code example from this page and paste it on the specified lines in the file.
1 from pymongo import MongoClient 2 3 try: 4 # start example code here 5 6 # end example code here 7 8 client.admin.command("ping") 9 print("Connected successfully") 10 11 # other application code 12 13 client.close() 14 15 except Exception as e: 16 raise Exception( 17 "The following error occurred: ", e)
Connection
Local Deployment
uri = "mongodb://localhost:27017/" client = MongoClient(uri)
Atlas
uri = "<Atlas connection string>" client = MongoClient(uri, server_api=pymongo.server_api.ServerApi( version="1", strict=True, deprecation_errors=True))
Replica Set
uri = "mongodb://<replica set member>:<port>/?replicaSet=<replica set name>" client = MongoClient(uri)
Network Compression
Compression Algorithms
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>", compressors = "snappy,zstd,zlib")
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "compressors=snappy,zstd,zlib") client = pymongo.MongoClient(uri)
To learn more about specifying compression algorithms, see Specify Compression Algorithms in the Network Compression guide.
zlib Compression Level
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>", compressors = "zlib", zlibCompressionLevel=<zlib compression level>)
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "compressors=zlib" "zlibCompressionLevel=<zlib compression level>") client = pymongo.MongoClient(uri)
To learn more about setting the zlib compression level, see Specify Compression Algorithms in the Network Compression guide.
Server Selection
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>", server_selector=<selector function>)
To learn more about customizing server selection, see Customize Server Selection.
Stable API
from pymongo.server_api import ServerApi client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>", server_api=ServerApi("<Stable API version>"))
To learn more about the Stable API, see Stable API.
Limit Server Execution Time
timeout Block
with pymongo.timeout(<timeout length>): # perform operations here
To learn more about client-side timeouts, see Limit Server Execution Time.
timeoutMS Connection Option
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname@:<port>", timeoutMS=<timeout length>)
uri = "mongodb://<db_username>:<db_password>@<hostname:<port>/?timeoutMS=<timeout length>" client = pymongo.MongoClient(uri)
To learn more about client-side timeouts, see Limit Server Execution Time.