Docs Menu
Docs Home
/
MongoDB Drivers
/

Motor (Async Driver)

On this page

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

Welcome to the documentation site for Motor, the official MongoDB driver for asynchronous Python applications. Download it using pip or set up a runnable project by following our tutorials.

Tip

If you do not need to access MongoDB in a non-blocking manner or from co-routines, we recommend that you use the PyMongo driver instead.

Follow the links below to read blog posts that describe specific use cases for the Motor driver:

You must install the Motor driver module to make it available to your Python application. We recommend using pip to install Motor.

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

$ python -m pip install motor

For more information on requirements and other methods of installation, see the Motor Installation documentation.

You can use the following connection snippet to test your connection to your MongoDB deployment on Atlas using the asyncio asynchronous framework:

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo.server_api import ServerApi
async def ping_server():
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Set the Stable API version when creating a new client
client = AsyncIOMotorClient(uri, server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
await client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
asyncio.run(ping_server())

This connection snippet uses the Stable API feature, which you can enable when using the Motor driver v2.5 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:

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
async def ping_server():
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Create a new client and connect to the server
client = AsyncIOMotorClient(uri)
# Send a ping to confirm a successful connection
try:
await client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
asyncio.run(ping_server())

If you are using the tornado asynchronous library, you can use the following code to connect to your MongoDB deployment:

import tornado
import motor
async def ping_server():
# Replace the placeholder with your Atlas connection string
uri = "<connection string>"
# Set a 5-second connection timeout when creating a new client
client = motor.motor_tornado.MotorClient(uri, serverSelectionTimeoutMS=5000)
# Send a ping to confirm a successful connection
try:
await client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
tornado.ioloop.IOLoop.current().run_sync(ping_server)

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 or versions of the Motor (Python async) 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.
Motor (Python async) 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
3.2 to 3.5
3.1
3.0

The driver does not support older versions of MongoDB.

The following compatibility table specifies the recommended version(s) of the Motor (Python async) driver for use with a specific version of Python.

The first column lists the driver version(s).

Motor (Python async) Driver Version
Python 3.12
Python 3.11
Python 3.10
Python 3.9
Python 3.8
Python 3.7
3.5
3.3 to 3.4
3.1 to 3.2
3.0
  • Motor 3.5 wraps PyMongo 4.5 to 4.8

  • Motor 3.3 and 3.4 wrap PyMongo 4.5

  • Motor 3.2 wraps PyMongo 4.4+

  • Motor 3.1 wraps PyMongo 4.2+

  • Motor 3.0 wraps PyMongo 4.1+

Note

  • For asyncio support, Motor requires Python 3.4+, or Python 3.3 with the asyncio package from PyPI.

  • Motor 2.3+ supports Windows.

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

← Python Drivers