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
You can use the following connection snippet to test your connection to your MongoDB deployment on Atlas:
from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi # Replace the placeholder with your Atlas connection string uri = "<connection string>" # Set the Stable API version when creating a new client client = MongoClient(uri, server_api=ServerApi('1')) # Send a ping to confirm a successful connection try: client.admin.command('ping') print("Pinged your deployment. You successfully connected to MongoDB!") except Exception as e: print(e)
This connection snippet uses the Stable API feature, which you can enable when using the PyMongo driver v3.12 and later to connect to MongoDB Server v5.0 and later. When you use this feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.
To learn more about the Stable API feature, see Stable API in the Server manual.
Note
Starting from Feburary 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.
Connect to MongoDB Atlas Without the Stable API
If you are using a version of MongoDB or the driver that doesn't support the Stable API feature, you can use the following code snippet to test your connection to your MongoDB deployment on Atlas:
from pymongo.mongo_client import MongoClient # Replace the placeholder with your Atlas connection string uri = "<connection string>" # Create a new client and connect to the server client = MongoClient(uri) # Send a ping to confirm a successful connection try: client.admin.command('ping') print("Pinged your deployment. You successfully connected to MongoDB!") except Exception as e: print(e)
Connect to MongoDB Atlas from AWS Lambda
To learn how to connect to Atlas from AWS Lambda, see the Manage Connections with AWS Lambda documentation.
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.