You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/content/driver/getting-started/quick-start.md
+64-8Lines changed: 64 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,9 +23,24 @@ that can be found with the driver source on github.
23
23
24
24
- The following import statements:
25
25
26
+
New MongoClient API (since 3.7):
27
+
28
+
```java
29
+
importcom.mongodb.ConnectionString;
30
+
importcom.mongodb.clients.MongoClients;
31
+
importcom.mongodb.clients.MongoClient;
32
+
```
33
+
34
+
Legacy MongoClient API:
35
+
26
36
```java
27
37
importcom.mongodb.MongoClient;
28
38
importcom.mongodb.MongoClientURI;
39
+
```
40
+
41
+
And the common elements between the legacy and new APIs:
42
+
43
+
```java
29
44
importcom.mongodb.ServerAddress;
30
45
31
46
importcom.mongodb.client.MongoDatabase;
@@ -45,7 +60,9 @@ import java.util.List;
45
60
```
46
61
## Make a Connection
47
62
48
-
Use [`MongoClient()`]({{< apiref "com/mongodb/MongoClient.html">}}) to make a connection to a running MongoDB instance.
63
+
Use [`MongoClients.create()`]({{< apiref "com/mongodb/client/MongoClients.html">}}),
64
+
or [`MongoClient()`]({{< apiref "com/mongodb/MongoClient.html">}}) for the legacy MongoClient API,
65
+
to make a connection to a running MongoDB instance.
49
66
50
67
The `MongoClient` instance represents a pool of connections to the database; you will only need one instance of class `MongoClient` even with multiple threads.
51
68
@@ -61,9 +78,46 @@ The `MongoClient` instance represents a pool of connections to the database; you
61
78
62
79
### Connect to a Single MongoDB instance
63
80
64
-
The following example shows five ways to connect to the
65
-
database `mydb` on the local machine. If the database does not exist, MongoDB
66
-
will create it for you.
81
+
The following example shows several ways to connect to a single MongoDB server.
82
+
83
+
##### New MongoClient API (since 3.7)
84
+
85
+
To connect to a single MongoDB instance:
86
+
87
+
- You can instantiate a MongoClient object without any parameters to connect to a MongoDB instance running on localhost on port ``27017``:
88
+
89
+
```java
90
+
MongoClient mongoClient =MongoClients.create();
91
+
```
92
+
93
+
- You can explicitly specify the hostname to connect to a MongoDB instance running on the specified host on port ``27017``:
The connection string mostly follows [RFC 3986](http://tools.ietf.org/html/rfc3986), with the exception of the domain name. For MongoDB, it is possible to list multiple domain names separated by a comma. For more information on the connection string, see [connection string]({{< docsref "reference/connection-string" >}}).
150
+
The connection string mostly follows [RFC 3986](http://tools.ietf.org/html/rfc3986), with the exception of the domain name. For MongoDB,
151
+
it is possible to list multiple domain names separated by a comma. For more information on the connection string, see
Copy file name to clipboardExpand all lines: docs/reference/content/driver/tutorials/aggregation.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,8 @@ The [aggregation pipeline]({{<docsref "core/aggregation-pipeline">}}) is a frame
20
20
21
21
```java
22
22
importcom.mongodb.Block;
23
-
importcom.mongodb.MongoClient;
23
+
importcom.mongodb.client.MongoClients;
24
+
importcom.mongodb.client.MongoClient;
24
25
importcom.mongodb.client.MongoCollection;
25
26
importcom.mongodb.client.MongoDatabase;
26
27
importcom.mongodb.client.model.Aggregates;
@@ -49,7 +50,7 @@ Connect to a MongoDB deployment and declare and define a `MongoDatabase` and a `
49
50
For example, include the following code to connect to a standalone MongoDB deployment running on localhost on port `27017` and define `database` to refer to the `test` database and `collection` to refer to the `restaurants` collection.
0 commit comments