Navigation

Motor (Async Driver)

On this page

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

Motor is the recommended asynchronous Python driver for MongoDB. It is compatible with Tornado and asyncio. We recommend that you use this driver if you need to run asynchronous operations. 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 learn more about how to use the Motor driver:

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.

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.

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.

Motor (Python async) Driver Version
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6
2.4
2.3
2.2
2.1
2.0
1.3
1.2
1.1
1.0
  • 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+

The driver does not support older versions of MongoDB.

Motor (Python async) Driver Version
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
2.4
2.3
2.2
2.1
2.0
1.3
1.2
1.1
1.0
  • 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.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.

Give Feedback

On this page

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