Motor (Async Driver)
On this page
Introduction
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:
Installation
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.
Connect to MongoDB Atlas
To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:
If you are using the asyncio
asynchronous framework, you can use the
following code to connect:
import asyncio import motor.motor_asyncio async def get_server_info(): # replace this with your MongoDB connection string conn_str = "<your MongoDB Atlas connection string>" # set a 5-second connection timeout client = motor.motor_asyncio.AsyncIOMotorClient(conn_str, serverSelectionTimeoutMS=5000) try: print(await client.server_info()) except Exception: print("Unable to connect to the server.") loop = asyncio.get_event_loop() loop.run_until_complete(get_server_info())
If you are using the tornado
asynchronous library, you can use the
following code to connect:
import tornado import motor async def get_server_info(): # replace this with your MongoDB connection string conn_str = "<your MongoDB Atlas connection string>" # set a 5-second connection timeout client = motor.motor_tornado.MotorClient(conn_str, serverSelectionTimeoutMS=5000) try: print(await client.server_info()) except Exception: print("Unable to connect to the server.") tornado.ioloop.IOLoop.current().run_sync(get_server_info)
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.
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 Motor (Python async) 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. |
Motor (Python async) 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 |
---|---|---|---|---|---|---|---|---|---|---|
3.1 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
3.0 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
2.5 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
2.4 | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
2.3 | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
2.2 | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
2.1 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
2.0 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
1.3 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||
1.2 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||
1.1 | ✓ | ✓ | ✓ | ✓ | ||||||
1.0 | ✓ | ✓ | ✓ | ✓ |
The driver does not support older versions of MongoDB.
Language Compatibility
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.11 | Python 3.10 | Python 3.9 | Python 3.8 | Python 3.7 | Python 3.6 | Python 3.5.2 | Python 3.5.0 | Python 3.4 | Python 3.3 | Python 2.7 | Python 2.6 | Python 2.5 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3.1 | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||||
3.0 | ✓ | ✓ | ✓ | ✓ | |||||||||
2.5 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
2.4 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
2.3 | ✓ | ✓ | ✓ | ✓ | |||||||||
2.2 | ✓ | ✓ | ✓ | ✓ | |||||||||
2.1 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
2.0 | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||||
1.3 | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||||
1.2 | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||||
1.1 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||||
1.0 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Motor 3.1 wraps PyMongo 4.2+
Motor 3.0 wraps PyMongo 4.1+
Motor 2.4 wraps PyMongo 3.11+
Motor 2.3 wraps PyMongo 3.11+
Motor 2.2 wraps PyMongo 3.11+
Motor 2.1 wraps PyMongo 3.10+
Motor 2.0 wraps PyMongo 3.7+
Motor 1.3 wraps PyMongo 3.6+ and adds support for CPython 3.7
Motor 1.2 wraps PyMongo 3.6+
Motor 1.1 wraps PyMongo 3.4+
Motor 1.0 wraps PyMongo 3.3+
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.
How to get help
Ask questions on our MongoDB Community Forums.
Visit our Support Channels.
See JIRA to raise issues or request features.