Skip to content

Commit 64cef5f

Browse files
author
Chris Cho
authored
DOCSP-17443: versioned api client instantiation snippets - Part 2 (#743)
* DOCSP-17443: versioned api client instantiation snippets for PyMongo, Scala, and Reactive Streams
1 parent 2f0b9da commit 64cef5f

File tree

7 files changed

+102
-7
lines changed

7 files changed

+102
-7
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import com.mongodb.ConnectionString;
2+
import com.mongodb.MongoClientSettings;
3+
import com.mongodb.ServerApi;
4+
import com.mongodb.ServerApiVersion;
5+
import com.mongodb.reactivestreams.client.MongoClient;
6+
import com.mongodb.reactivestreams.client.MongoClients;
7+
8+
// ...
9+
10+
// Replace <connection string> with your MongoDB deployment's connection string.
11+
ConnectionString connString = new ConnectionString("<connection string>");
12+
13+
// Set the version of the Versioned API on the client.
14+
ServerApi serverApi = ServerApi.builder()
15+
.version(ServerApiVersion.V1)
16+
.build();
17+
18+
MongoClientSettings settings = MongoClientSettings.builder()
19+
.applyConnectionString(connString)
20+
.serverApi(serverApi)
21+
.build();
22+
MongoClient mongoClient = MongoClients.create(settings);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pymongo.mongo_client import MongoClient
2+
from pymongo.server_api import ServerApi
3+
4+
# Replace <connection string> with your MongoDB deployment's connection string.
5+
conn_str = "<connection string>"
6+
7+
# Set the version of the Versioned API on the client.
8+
client = pymongo.MongoClient(conn_str, server_api=ServerApi('1'), serverSelectionTimeoutMS=5000)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import com.mongodb.{ServerApi, ServerApiVersion}
2+
import org.mongodb.scala._
3+
4+
// ...
5+
6+
// Replace <connection string> with your MongoDB deployment's connection string.
7+
val uri = "<connection string>"
8+
9+
// Set the version of the Versioned API on the client.
10+
val mongoClientSettings = MongoClientSettings.builder().applyConnectionString(ConnectionString(uri))
11+
.serverApi(ServerApi.builder().version(ServerApiVersion.V1).build())
12+
.build()
13+
14+
val mongoClient = MongoClient(mongoClientSettings)

source/pymongo.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ Connect to MongoDB Atlas
7575

7676
import pymongo
7777

78-
# replace this with your MongoDB connection string
79-
conn_str = "<your MongoDB Atlas connection string>"
78+
# Replace the uri string with your MongoDB deployment's connection string.
79+
conn_str = "mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority"
8080

8181
# set a 5-second connection timeout
8282
client = pymongo.MongoClient(conn_str, serverSelectionTimeoutMS=5000)
@@ -101,6 +101,19 @@ If the connection fails, you should see the following message:
101101
For more information on the connection string, see the MongoDB Server
102102
Manual entry on :manual:`Connection String URI Format </reference/connection-string/>`.
103103

104+
Versioned API
105+
-------------
106+
107+
You can use the `Versioned API <https://docs.mongodb.com/v5.0/reference/versioned-api/>`__
108+
feature starting with MongoDB Server version 5.0 and PyMongo Driver
109+
version 3.12. When you use the Versioned API feature, you can update your
110+
driver or server without worrying about backward compatibility issues with any
111+
commands covered by the Versioned API. To use this feature, construct a
112+
MongoDB client instance, specifying a version of the Versioned API:
113+
114+
.. literalinclude:: /includes/versioned-api-snippets/pymongo-client.py
115+
:language: python
116+
104117
Connect to ``localhost``
105118
------------------------
106119

source/reactive-streams.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Connect to MongoDB Atlas
5050
import com.mongodb.MongoClientSettings;
5151

5252
// ...
53+
54+
// Replace the uri string with your MongoDB deployment's connection string.
5355
ConnectionString connString = new ConnectionString(
5456
"mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
5557
);
@@ -65,6 +67,19 @@ Connect to MongoDB Atlas
6567
See :java-docs:`Connect to MongoDB <driver-reactive/tutorials/connect-to-mongodb/>`
6668
for more ways to connect.
6769

70+
Versioned API
71+
-------------
72+
73+
You can use the `Versioned API <https://docs.mongodb.com/v5.0/reference/versioned-api/>`__
74+
feature starting with MongoDB Server version 5.0 and Java Reactive Streams
75+
Driver version 4.3. When you use the Versioned API feature, you can update your
76+
driver or server without worrying about backward compatibility issues with any
77+
commands covered by the Versioned API. To use this feature, construct a
78+
MongoDB client instance, specifying a version of the Versioned API:
79+
80+
.. literalinclude:: /includes/versioned-api-snippets/java-rs-client.java
81+
:language: java
82+
6883
Connect to ``localhost``
6984
------------------------
7085

source/scala.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ It's a modern idiomatic Scala driver with asynchronous and non-blocking IO.
2323

2424

2525
- :java-docs:`Reference Documentation <driver-scala/>`
26-
26+
2727
- :java-docs:`Tutorials <driver-scala/tutorials/>`
2828

2929
- :java-docs:`API Documentation <apidocs/>`
@@ -53,6 +53,7 @@ Connect to MongoDB Atlas
5353

5454
// ...
5555

56+
// Replace the uri string with your MongoDB deployment's connection string.
5657
val uri: String = "mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority"
5758
System.setProperty("org.mongodb.async.type", "netty")
5859
val client: MongoClient = MongoClient(uri)
@@ -63,6 +64,19 @@ Connect to MongoDB Atlas
6364
See our guide on :java-docs:`Connecting <driver-scala/tutorials/connect-to-mongodb/>`
6465
for more ways to connect.
6566

67+
Versioned API
68+
-------------
69+
70+
You can use the `Versioned API <https://docs.mongodb.com/v5.0/reference/versioned-api/>`__
71+
feature starting with MongoDB Server version 5.0 and Scala Driver version 4.3.
72+
When you use the Versioned API feature, you can update your driver or server
73+
without worrying about backward compatibility issues with any commands covered
74+
by the Versioned API. To use this feature, construct a MongoDB client
75+
instance, specifying a version of the Versioned API:
76+
77+
.. literalinclude:: /includes/versioned-api-snippets/scala-client.scala
78+
:language: scala
79+
6680
Connect to ``localhost``
6781
------------------------
6882

source/swift.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ Connect to MongoDB Atlas
5353
cleanupMongoSwift()
5454
}
5555

56-
let client = try MongoClient("mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority")
57-
let db = client.db("myDB")
56+
// replace the following string with your connection uri
57+
let uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
58+
let client = try MongoClient(uri)
59+
60+
// print a list of database names
61+
print (try client.listDatabaseNames())
5862

5963
// your application logic
6064

@@ -67,8 +71,12 @@ Connect to MongoDB Atlas
6771
import NIO
6872

6973
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
74+
75+
// replace the following string with your connection uri
76+
let uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
77+
7078
let client = try MongoClient(
71-
"mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority",
79+
uri,
7280
using: elg
7381
)
7482

@@ -81,7 +89,8 @@ Connect to MongoDB Atlas
8189
try? elg.syncShutdownGracefully()
8290
}
8391

84-
let db = client.db("myDB")
92+
// print a list of database names
93+
print(try client.listDatabaseNames().wait())
8594

8695
// your application logic
8796

0 commit comments

Comments
 (0)