Docs Menu

Docs HomeDevelop ApplicationsMongoDB Drivers

PyMongo

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Connect to MongoDB Atlas Without the Stable API
  • Connect to MongoDB Atlas from AWS Lambda
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

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.

You can use the PyMongo driver to connect to deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB

  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB

Banner for the MongoDB University Python Course

Using MongoDB with Python

Learn the essentials of Python application development with MongoDB.

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.

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 February 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.

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)

To learn how to connect to Atlas from AWS Lambda, see the Manage Connections with AWS Lambda documentation.

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:

  1. Download the Community or Enterprise version of MongoDB Server.

  2. Install and configure MongoDB Server.

  3. 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.

Important

Supported PyMongo and Python Versions

MongoDB does not support any Python driver versions earlier than v4.0 or any Python versions earlier than v3.6.

The following compatibility table specifies the recommended version or versions of the MongoDB Python driver for use with a specific version of MongoDB.

The first column lists the driver version.

Important

MongoDB ensures compatibility between the MongoDB Server and the drivers for three years after the server version's end of life (EOL) date. To learn more about the MongoDB release and EOL dates, see MongoDB Software Lifecycle Schedules.

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 7.0
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.6
4.5
4.4
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.

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).

PyMongo Driver Version
Python 3.12
Python 3.11
Python 3.10 [1]
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.6
4.5
4.4
4.3
4.2
4.1
[2]
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
[1] 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.
[2] Pymongo 4.1 requires Python 3.6.2 or later.

Note

PyPy3 is a Python 3.2-compatible alternative interpreter.

PyMongo Driver Version
Python 2.7, PyPy
Python 2.6
Python 2.5, Jython 2.5
Python 2.4
4.0 [3]
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
[3] Versions of PyMongo 4.0 and later are not compatible with Python 2

Note

Jython 2.5 is a Python 2.5-compatible alternative interpreter.

PyPy is a Python 2.7-compatible alternative interpreter.

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.

←  MongoDB Python DriversMotor (Async Driver) →