PyMongo
On this page
Introduction
Welcome to the documentation site for PyMongo, the official MongoDB driver for synchronous Python applications. Download it using pip or set up a runnable project by following our tutorial.
Tip
If you need to access MongoDB in a non-blocking manner or from co-routines, we recommend that you use the Motor driver instead.
Tutorial on how to connect to MongoDB and run common operations.
Take the Free Online Course Taught by MongoDB
![]() | Learn the essentials of Python application development with MongoDB. |
Installation
You must install the PyMongo driver module to make it available to your Python application. We recommend using pip to install PyMongo.
PyMongo requires dnspython to support
mongodb+srv://
connection strings and MongoDB Atlas. dnspython
is automatically installed
when installing or upgrading to the latest PyMongo version.
The following command demonstrates how you can install the latest version of the module using the command line:
python -m pip install pymongo
If you need to install a specific version of PyMongo, specify the
version in your command. The following command shows how you can use
pip
to install PyMongo version 3.11
:
python -m pip install pymongo==3.11
If you already have PyMongo installed and need to upgrade to the latest
version, use the following pip
command:
python -m pip install --upgrade pymongo
See Installation for more ways to install PyMongo.
Connect to MongoDB Atlas
To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:
import pymongo # Replace the uri string with your MongoDB deployment's connection string. conn_str = "mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority" # set a 5-second connection timeout client = pymongo.MongoClient(conn_str, serverSelectionTimeoutMS=5000) try: print(client.server_info()) except Exception: print("Unable to connect to the server.")
Note
For information about connecting to Atlas Serverless, see the Serverless Instance Limitations page for the minimum driver version you need.
If the connection succeeds before a five-second timeout, you will see a dictionary containing information about the server you connected to.
If the connection fails, you should see the following message:
Unable to connect to the server.
For more information on the connection string, see the MongoDB Server Manual entry on Connection String URI Format.
Stable API
You can use the Stable API feature starting with MongoDB Server version 5.0 and PyMongo Driver version 3.12. When you use the Stable API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.
Note
Starting from Feburary 2022, the Versioned API is known the Stable API. All concepts and features remain the same with this naming change.
To use this feature, construct a MongoDB client instance, specifying a version of the Stable API:
from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi # Replace <connection string> with your MongoDB deployment's connection string. conn_str = "<connection string>" # Set the Stable API version on the client. client = pymongo.MongoClient(conn_str, server_api=ServerApi('1'), serverSelectionTimeoutMS=5000)
Connect to a MongoDB Server on Your Local Machine
If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:
Download the Community or Enterprise version of MongoDB Server.
Install and configure MongoDB Server.
Start the server.
Important
Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.
After you successfully start your MongoDB server, specify your connection string in your driver connection code.
If your MongoDB Server is running locally, you can use the connection string
"mongodb://localhost:<port>"
where <port>
is the port number you
configured your server to listen for incoming connections.
If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.
To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.
Compatibility
MongoDB Compatibility
The following compatibility table specifies the recommended version(s) of the MongoDB Python driver for use with a specific version of MongoDB.
The first column lists the driver version(s).
Compatibility Table Legend
Icon | Explanation |
---|---|
✓ | All features are supported. |
⊛ | The Driver version will work with the MongoDB version, but not all
new MongoDB features are supported. |
No mark | The Driver version is not tested with the MongoDB version. |
PyMongo Driver Version | MongoDB 6.0 | MongoDB 5.0 | MongoDB 4.4 | MongoDB 4.2 | MongoDB 4.0 | MongoDB 3.6 | MongoDB 3.4 | MongoDB 3.2 | MongoDB 3.0 | MongoDB 2.6 |
---|---|---|---|---|---|---|---|---|---|---|
4.3 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.2 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.1 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.0 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
3.13 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.12 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.11 | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.10 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.9 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.8 | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.7 | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
3.6 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||
3.5 | ✓ | ✓ | ✓ | ✓ | ||||||
3.4 | ✓ | ✓ | ✓ | ✓ | ||||||
3.3 | ✓ | ✓ | ✓ | |||||||
3.2 | ✓ | ✓ | ✓ | |||||||
3.1 | ✓ | ✓ | ||||||||
3.0 | ✓ | ✓ | ||||||||
2.9 | ✓ | ✓ | ||||||||
2.8 | ✓ | ✓ | ||||||||
2.7 | ✓ |
The driver does not support older versions of MongoDB.
Language Compatibility
The following compatibility table specifies the recommended version(s) of the MongoDB Python driver for use with a specific version of Python.
The first column lists the driver version(s).
Python 3 Compatibility
PyMongo Driver Version | Python 3.11 | Python 3.10 [2] | Python 3.9 | Python 3.8 | Python 3.7 | Python 3.6 | Python 3.5 | Python 3.4 | Python 3.3 | PyPy3 | Python 3.2 | Python 3.1 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
4.3 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
4.2 | ✓ | ✓ | ✓ | ✓ | ||||||||
4.1 | ✓ | ✓ | ✓ | ✓ | ✓ [3] | |||||||
4.0 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
3.13 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||
3.12 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
3.11 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||||
3.10 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||
3.9 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
3.8 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
3.7 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
3.6 | ✓ | ✓ | ✓ | ✓ | ||||||||
3.5 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
3.4 | ✓ | ✓ | ✓ | ✓ | ||||||||
3.3 | ✓ | ✓ | ✓ | ✓ | ||||||||
3.2 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
3.1 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
3.0 | ✓ | ✓ | ✓ | ✓ | ||||||||
2.9 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||||
2.8 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
2.7 | ✓ | ✓ | ✓ | ✓ | ✓ |
Python 2 Compatibility
[1] | (1, 2) Versions of PyMongo 4.0 and later are not compatible with Python 2 |
[2] | Versions of Python 3.10 and later are not compatible with TLS/SSL for versions of MongoDB 4.0 and earlier. See the PyMongo documentation for more information. |
[3] | Pymongo 4.1 requires Python 3.6.2 or later. |
Note
Jython 2.5 is a Python 2.5-compatible alternative interpreter.
PyPy is a Python 2.7 and 3.2-compatible alternative interpreter.
For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.
How to get help
Ask questions on our MongoDB Community Forums.
Visit our Support Channels.
See JIRA to raise issues or request features.