Docs Menu

PyMongo

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Stable API
  • 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.

Banner for the M220P MongoDB University Course

M220P: MongoDB for Python Developers

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.

The following command demonstrates how you can install the latest version of the module using the command line:

$ python -m pip install 'pymongo[srv]'

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[srv]'==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[srv]'

See Installation for more ways to install PyMongo.

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.

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 version of the {+stable-api+} on the client.
client = pymongo.MongoClient(conn_str, server_api=ServerApi('1'), serverSelectionTimeoutMS=5000)

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.

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

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.2.0b0
4.1
4.0
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.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.2.0b0

4.1

[3]
4.0

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

PyMongo Driver Version
Python 2.7, PyPy
Python 2.6
Python 2.5, Jython 2.5
Python 2.4
4.1 [1]
4.0 [1]
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](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.

←  MongoDB Python DriversMotor (Async Driver) →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.