@@ -17,49 +17,117 @@ Motor (Async Driver)
17
17
Introduction
18
18
------------
19
19
20
- **Motor (Python async)** is the recommended asynchronous Python driver for MongoDB.
21
- It is compatible with `Tornado <http://www.tornadoweb.org/>`_ and
22
- `asyncio <https://docs.python.org/3/library/asyncio.html>`_.
20
+ **Motor** is the recommended asynchronous Python driver for MongoDB.
21
+ It is compatible with `Tornado <http://www.tornadoweb.org/>`__ and
22
+ `asyncio <https://docs.python.org/3/library/asyncio.html>`__. We recommend
23
+ that you use this driver if you need to run asynchronous operations. If
24
+ you do not need to access MongoDB in a non-blocking manner or from
25
+ co-routines, we recommend that you use the :doc:`PyMongo </pymongo>`
26
+ driver instead.
23
27
24
- - `Tutorial <http://motor.readthedocs.org/en/stable/tutorial.html>`_
28
+ Follow the links below to learn more about how to use the Motor driver:
25
29
26
- - `Documentation <http://motor.readthedocs.org/>`_
30
+ - `Tutorial on using Motor with Tornado <http://motor.readthedocs.org/en/stable/tutorial-tornado.html>`__
31
+
32
+ - `Tutorial on using Motor with asyncio <https://motor.readthedocs.io/en/stable/tutorial-asyncio.html>`__
33
+
34
+ - `Motor Documentation <http://motor.readthedocs.org/>`__
27
35
28
36
- `Changelog <http://motor.readthedocs.org/en/stable/changelog.html>`__
29
37
30
38
- `Source Code <https://github.com/mongodb/motor/>`__
31
39
32
- - `Porting From PyMongo To Motor <http://emptysqua.re/blog/porting-from-pymongo-to-motor/>`_
40
+ Follow the links below to read blog posts that describe specific use cases
41
+ for the Motor driver:
33
42
34
- - `Refactoring Tornado Coroutines <http://emptysqua.re/blog/refactoring-tornado-coroutines />`_
43
+ - `Porting From PyMongo To Motor <http://emptysqua.re/blog/porting-from-pymongo-to-motor />`__
35
44
36
- - `All Motor articles on A. Jesse Jiryu Davis's blog <http://emptysqua.re/blog/category/motor/>`_
45
+ - `Refactoring Tornado Coroutines <http://emptysqua.re/blog/refactoring-tornado-coroutines/>`__
37
46
47
+ - `All Motor articles on A. Jesse Jiryu Davis's blog <http://emptysqua.re/blog/category/motor/>`__
38
48
39
49
Installation
40
50
------------
41
51
42
- We recommend using `pip <http://pypi.python.org/pypi/pip>`__ to install
43
- Motor on all platforms:
52
+ You must install the Motor driver module to make it available to your Python
53
+ application. We recommend using `pip <http://pypi.python.org/pypi/pip>`__
54
+ to install Motor.
55
+
56
+ The following command demonstrates how you can install the latest version of
57
+ the module using the command line:
44
58
45
59
.. code-block:: sh
46
60
47
- $ pip install motor
61
+ $ python -m pip install motor
48
62
63
+ For more information on requirements and other methods of installation,
64
+ see the `Motor Installation <https://motor.readthedocs.io/en/stable/installation.html>`__
65
+ documentation.
49
66
50
67
Connect to MongoDB Atlas
51
68
------------------------
52
69
53
70
.. include:: /includes/atlas-connect-blurb.rst
54
71
72
+ If you are using the ``asyncio`` asynchronous framework, you can use the
73
+ following code to connect:
74
+
55
75
.. code-block:: python
56
76
77
+ import asyncio
78
+ import motor.motor_asyncio
79
+
80
+ async def get_server_info():
81
+
82
+ # replace this with your MongoDB connection string
83
+ conn_str = "<your MongoDB Atlas connection string>"
84
+
85
+ # set a 5-second connection timeout
86
+ client = motor.motor_asyncio.AsyncIOMotorClient(conn_str, serverSelectionTimeoutMS=5000)
87
+
88
+ try:
89
+ print(await client.server_info())
90
+ except Exception:
91
+ print("Unable to connect to the server.")
92
+
93
+ loop = asyncio.get_event_loop()
94
+ loop.run_until_complete(get_server_info())
95
+
96
+ If you are using the ``tornado`` asynchronous library, you can use the
97
+ following code to connect:
98
+
99
+ .. code-block:: python
100
+
101
+ import tornado
57
102
import motor
58
103
59
- client = motor.motor_tornado.MotorClient(
60
- "mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority")
61
- db = client.test
104
+ async def get_server_info():
105
+
106
+ # replace this with your MongoDB connection string
107
+ conn_str = "<your MongoDB Atlas connection string>"
108
+
109
+ # set a 5-second connection timeout
110
+ client = motor.motor_tornado.MotorClient(conn_str, serverSelectionTimeoutMS=5000)
111
+
112
+ try:
113
+ print(await client.server_info())
114
+ except Exception:
115
+ print("Unable to connect to the server.")
116
+
117
+ tornado.ioloop.IOLoop.current().run_sync(get_server_info)
118
+
119
+ If the connection succeeds before a five-second timeout, you will see a
120
+ dictionary containing information about the server you connected to.
121
+
122
+ If the connection fails, you should see the following message:
123
+
124
+ .. code-block:: console
125
+ :copyable: false
126
+
127
+ Unable to connect to the server.
62
128
129
+ For more information on the connection string, see the MongoDB Server
130
+ Manual entry on :manual:`Connection String URI Format </reference/connection-string/>`.
63
131
64
132
Compatibility
65
133
-------------
0 commit comments