Docs Menu

PyMongo

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Versioned API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

PyMongo is the official MongoDB Python driver for MongoDB. We recommend you use this driver to work with MongoDB from Python. If you need to access MongoDB in a non-blocking manner or from co-routines, we recommend that you use the Motor driver instead.

Follow the links below to learn more about how to use the PyMongo driver:

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 Versioned API feature starting with MongoDB Server version 5.0 and PyMongo Driver version 3.12. When you use the Versioned API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Versioned API. To use this feature, construct a MongoDB client instance, specifying a version of the Versioned 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 Versioned 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.

Pymongo Driver Version
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
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.

Pymongo Driver Version
Python 2.7, PyPy
Python 2.6
Python 2.5, Jython 2.5
Python 2.4
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 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
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

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.

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.