Skip to content
1 change: 1 addition & 0 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
[constants]
driver-short = "PyMongo"
driver-long = "PyMongo, the MongoDB synchronous Python driver,"
driver-async = "PyMongo Async"
language = "Python"
mdb-server = "MongoDB Server"
mongo-community = "MongoDB Community Edition"
Expand Down
17 changes: 17 additions & 0 deletions source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ MongoDB {+driver-short+} Documentation
/troubleshooting
/whats-new
/upgrade
/motor-async-migration
/pymongo-to-async-guide
/previous-versions
/issues-and-help
/compatibility
Expand Down Expand Up @@ -120,6 +122,21 @@ Upgrade {+driver-short+} Versions
Learn what changes you might need to make to your application to upgrade driver versions
in the :ref:`pymongo-upgrade` section.

Migrate from Motor to {+driver-async+}
-------------------------------------

In September 2024, MongoDB released the {+driver-async+} driver as a replacement
Copy link
Member

@ShaneHarvey ShaneHarvey Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Async PyMongo is released yes, but it's only in beta (ie experimental) and not ready for production use. We should make this loud and clear one every one of these pages.

for `Motor <https://www.mongodb.com/docs/drivers/motor/>`__, the previous asynchronous
MongoDB driver for Python. Learn how to migrate from Motor
to the {+driver-async+} driver in the :ref:`pymongo-async-motor-migration`
section.

Switch from {+driver-short+} to {+driver-async+}
----------------------------------------------

Learn what changes you need to make to switch from {+driver-short+} to
{+driver-async+} in the :ref:`pymongo-to-async-guide` section.

Previous Versions
-----------------

Expand Down
74 changes: 74 additions & 0 deletions source/motor-async-migration.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
.. _pymongo-async-motor-migration:

====================================
Migrate from Motor to {+driver-async+}
====================================

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: motor, async, refactor, migration

Overview
--------

The {+driver-async+} driver is a unification of {+driver-short+} and the `Motor
library <https://www.mongodb.com/docs/drivers/motor/>`__. In this guide, you can
identify the changes you must make to migrate an application from
Motor to the {+driver-async+} driver.

Migrate From Motor
------------------

The {+driver-async+} driver functions similarly to the Motor library, but allows
for improved latency and throughput due to directly using Python Asyncio instead
of delegating work to a thread pool. In most cases, you can directly migrate
existing Motor applications to {+driver-async+} by using ``AsyncMongoClient`` in
place of ``MotorClient``, and changing the application's import statements to
import from ``pymongo``.

The following example shows the difference in imports to use a client for
read and write operations in Motor compared to {+driver-async+}:

.. code-block:: python

# Motor client import
from motor.motor_asyncio import AsyncIOMotorClient

# {+driver-async+} client import
from pymongo import AsyncMongoClient

To see a list of the asynchronous methods available in the {+driver-async+}
driver, see the :ref:`pymongo-async-methods` section in the {+driver-short+} to
{+driver-async+} guide.

The following section shows the method signature changes that you must implement
in your application when migrating from Motor to the {+driver-async+} driver.

Method Signature Changes
~~~~~~~~~~~~~~~~~~~~~~~~

The following Motor method signatures behave differently in the {+driver-async+} driver:

- ``GridOut.open()`` returns ``None``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does GridOut here refer to a motor class? The pymongo class is AsyncGridOut.

- ``AsyncMongoClient.__init__()`` does not accept an ``io_loop`` parameter.
- ``GridIn.set()`` does not accept a filename. Instead pass a file name by using the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noah is this true? Does GridIn here refer to a motor class? The pymongo class is AsyncGridIn.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsyncGridIn.set() does accept filenames.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were from the original draft migration notes linked in the ticket. Should I delete the GridIn bullets?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

``GridIn.filename`` attribute.
- ``Cursor.each()`` does not exist in the {+driver-async+} driver.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor -> AsyncCursor.

- ``stream_to_handler()`` does not exist in the {+driver-async+} driver.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MotorGridOut.stream_to_handler()

- ``to_list(0)`` is not valid in the {+driver-async+} driver. Use
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsyncCursor.to_list(0)

``to_list(None)`` instead.

Additional Information
----------------------

To learn more about asynchronous Python, see the `Python Asyncio documentation
<https://docs.python.org/3/library/asyncio.html>`__.
Loading
Loading