Skip to content

Commit 91fde5a

Browse files
authored
DOCSP-28359: PyMongo and Motor stable api connection updates (#863)
1 parent 1fb6902 commit 91fde5a

File tree

7 files changed

+126
-90
lines changed

7 files changed

+126
-90
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import asyncio
2+
from motor.motor_asyncio import AsyncIOMotorClient
3+
4+
async def ping_server():
5+
# Replace the placeholder with your Atlas connection string
6+
uri = "<connection string>"
7+
8+
# Create a new client and connect to the server
9+
client = AsyncIOMotorClient(uri)
10+
11+
# Send a ping to confirm a successful connection
12+
try:
13+
client.admin.command('ping')
14+
print("Pinged your deployment. You successfully connected to MongoDB!")
15+
except Exception as e:
16+
print(e)
17+
18+
asyncio.run(ping_server())
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import tornado
2+
import motor
3+
4+
async def ping_server():
5+
# Replace the placeholder with your Atlas connection string
6+
uri = "<connection string>"
7+
8+
# Set a 5-second connection timeout when creating a new client
9+
client = motor.motor_tornado.MotorClient(uri, serverSelectionTimeoutMS=5000)
10+
11+
# Send a ping to confirm a successful connection
12+
try:
13+
client.admin.command('ping')
14+
print("Pinged your deployment. You successfully connected to MongoDB!")
15+
except Exception as e:
16+
print(e)
17+
18+
tornado.ioloop.IOLoop.current().run_sync(ping_server)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import asyncio
2+
from motor.motor_asyncio import AsyncIOMotorClient
3+
from pymongo.server_api import ServerApi
4+
5+
async def ping_server():
6+
# Replace the placeholder with your Atlas connection string
7+
uri = "<connection string>"
8+
9+
# Set the Stable API version when creating a new client
10+
client = AsyncIOMotorClient(uri, server_api=ServerApi('1'))
11+
12+
# Send a ping to confirm a successful connection
13+
try:
14+
client.admin.command('ping')
15+
print("Pinged your deployment. You successfully connected to MongoDB!")
16+
except Exception as e:
17+
print(e)
18+
19+
asyncio.run(ping_server())
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pymongo.mongo_client import MongoClient
2+
3+
# Replace the placeholder with your Atlas connection string
4+
uri = "<connection string>"
5+
6+
# Create a new client and connect to the server
7+
client = MongoClient(uri)
8+
9+
# Send a ping to confirm a successful connection
10+
try:
11+
client.admin.command('ping')
12+
print("Pinged your deployment. You successfully connected to MongoDB!")
13+
except Exception as e:
14+
print(e)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pymongo.mongo_client import MongoClient
2+
from pymongo.server_api import ServerApi
3+
4+
# Replace the placeholder with your Atlas connection string
5+
uri = "<connection string>"
6+
7+
# Set the Stable API version when creating a new client
8+
client = MongoClient(uri, server_api=ServerApi('1'))
9+
10+
# Send a ping to confirm a successful connection
11+
try:
12+
client.admin.command('ping')
13+
print("Pinged your deployment. You successfully connected to MongoDB!")
14+
except Exception as e:
15+
print(e)

source/motor.txt

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -68,69 +68,40 @@ documentation.
6868
Connect to MongoDB Atlas
6969
------------------------
7070

71-
.. include:: /includes/atlas-connect-blurb.rst
71+
You can use the following connection snippet to test your connection to
72+
your MongoDB deployment on Atlas using the ``asyncio`` asynchronous framework:
7273

73-
If you are using the ``asyncio`` asynchronous framework, you can use the
74-
following code to connect:
74+
.. literalinclude:: /includes/connection-snippets/scram/py-motor-connection.py
75+
:language: python
7576

76-
.. code-block:: python
77+
This connection snippet uses the {+stable-api+} feature, which you can
78+
enable when using the Motor driver v2.5 and later to connect to MongoDB Server
79+
v5.0 and later. When you use this feature, you can update your driver or server without
80+
worrying about backward compatibility issues with any commands covered by the
81+
{+stable-api+}.
7782

78-
import asyncio
79-
import motor.motor_asyncio
83+
To learn more about the {+stable-api+} feature, see
84+
:manual:`{+stable-api+} </reference/stable-api/>` in the Server manual.
8085

81-
async def get_server_info():
86+
.. include:: /includes/stable-api-notice.rst
8287

83-
# replace this with your MongoDB connection string
84-
conn_str = "<your MongoDB Atlas connection string>"
88+
.. _connect-atlas-no-stable-api-motor-driver:
8589

86-
# set a 5-second connection timeout
87-
client = motor.motor_asyncio.AsyncIOMotorClient(conn_str, serverSelectionTimeoutMS=5000)
90+
Connect to MongoDB Atlas Without the Stable API
91+
-----------------------------------------------
8892

89-
try:
90-
print(await client.server_info())
91-
except Exception:
92-
print("Unable to connect to the server.")
93+
If you are using a version of MongoDB or the driver that doesn't support the
94+
{+stable-api+} feature, you can use the following code snippet to test your connection
95+
to your MongoDB deployment on Atlas:
9396

94-
loop = asyncio.get_event_loop()
95-
loop.run_until_complete(get_server_info())
97+
.. literalinclude:: /includes/connection-snippets/scram/py-motor-connection-no-stableapi.py
98+
:language: python
9699

97100
If you are using the ``tornado`` asynchronous library, you can use the
98-
following code to connect:
101+
following code to connect to your MongoDB deployment:
99102

100-
.. code-block:: python
101-
102-
import tornado
103-
import motor
104-
105-
async def get_server_info():
106-
107-
# replace this with your MongoDB connection string
108-
conn_str = "<your MongoDB Atlas connection string>"
109-
110-
# set a 5-second connection timeout
111-
client = motor.motor_tornado.MotorClient(conn_str, serverSelectionTimeoutMS=5000)
112-
113-
try:
114-
print(await client.server_info())
115-
except Exception:
116-
print("Unable to connect to the server.")
117-
118-
tornado.ioloop.IOLoop.current().run_sync(get_server_info)
119-
120-
.. include:: /includes/serverless-compatibility.rst
121-
122-
If the connection succeeds before a five-second timeout, you will see a
123-
dictionary containing information about the server you connected to.
124-
125-
If the connection fails, you should see the following message:
126-
127-
.. code-block:: console
128-
:copyable: false
129-
130-
Unable to connect to the server.
131-
132-
For more information on the connection string, see the MongoDB Server
133-
Manual entry on :manual:`Connection String URI Format </reference/connection-string/>`.
103+
.. literalinclude:: /includes/connection-snippets/scram/py-motor-connection-tornado.py
104+
:language: python
134105

135106
Connect to a MongoDB Server on Your Local Machine
136107
-------------------------------------------------

source/pymongo.txt

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -77,52 +77,33 @@ for more ways to install PyMongo.
7777
Connect to MongoDB Atlas
7878
------------------------
7979

80-
.. include:: /includes/atlas-connect-blurb.rst
80+
You can use the following connection snippet to test your connection to
81+
your MongoDB deployment on Atlas:
8182

82-
.. code-block:: python
83-
84-
import pymongo
85-
86-
# Replace the uri string with your MongoDB deployment's connection string.
87-
conn_str = "mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority"
88-
89-
# set a 5-second connection timeout
90-
client = pymongo.MongoClient(conn_str, serverSelectionTimeoutMS=5000)
91-
92-
try:
93-
print(client.server_info())
94-
except Exception:
95-
print("Unable to connect to the server.")
96-
97-
.. include:: /includes/serverless-compatibility.rst
98-
99-
If the connection succeeds before a five-second timeout, you will see a
100-
dictionary containing information about the server you connected to.
101-
102-
If the connection fails, you should see the following message:
103-
104-
.. code-block:: console
105-
:copyable: false
83+
.. literalinclude:: /includes/connection-snippets/scram/pymongo-connection.py
84+
:language: python
10685

107-
Unable to connect to the server.
86+
This connection snippet uses the {+stable-api+} feature, which you can
87+
enable when using the PyMongo driver v3.12 and later to connect to MongoDB Server
88+
v5.0 and later. When you use this feature, you can update your driver or server without
89+
worrying about backward compatibility issues with any commands covered by the
90+
{+stable-api+}.
10891

109-
For more information on the connection string, see the MongoDB Server
110-
Manual entry on :manual:`Connection String URI Format </reference/connection-string/>`.
92+
To learn more about the {+stable-api+} feature, see
93+
:manual:`{+stable-api+} </reference/stable-api/>` in the Server manual.
11194

112-
{+stable-api+}
113-
--------------
95+
.. include:: /includes/stable-api-notice.rst
11496

115-
You can use the {+stable-api+} feature starting with MongoDB Server version
116-
5.0 and PyMongo Driver version 3.12. When you use the {+stable-api+} feature,
117-
you can update your driver or server without worrying about backward
118-
compatibility issues with any commands covered by the {+stable-api+}.
97+
.. _connect-atlas-no-stable-api-pymongo-driver:
11998

120-
.. include:: /includes/stable-api-notice.rst
99+
Connect to MongoDB Atlas Without the Stable API
100+
-----------------------------------------------
121101

122-
To use this feature, construct a MongoDB client instance, specifying a version
123-
of the {+stable-api+}:
102+
If you are using a version of MongoDB or the driver that doesn't support the
103+
{+stable-api+} feature, you can use the following code snippet to test your connection
104+
to your MongoDB deployment on Atlas:
124105

125-
.. literalinclude:: /includes/stable-api-snippets/pymongo-client.py
106+
.. literalinclude:: /includes/connection-snippets/scram/pymongo-connection-no-stableapi.py
126107
:language: python
127108

128109
Connect to a MongoDB Server on Your Local Machine

0 commit comments

Comments
 (0)