Skip to content

Commit fce0fe5

Browse files
committed
DOCSP-49053: Standardize MongoClient page
1 parent 012b476 commit fce0fe5

File tree

1 file changed

+55
-88
lines changed

1 file changed

+55
-88
lines changed

source/connect/mongoclient.txt

+55-88
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Create a MongoClient
1111

1212
.. meta::
1313
:keywords: connection string, URI, server, Atlas, settings
14+
:description: Learn how to create a `MongoClient` to connect to a MongoDB deployment using a connection URI and customize connection behavior.
1415

1516
.. contents:: On this page
1617
:local:
@@ -21,41 +22,57 @@ Create a MongoClient
2122
This guide shows you how to connect to a MongoDB instance or replica set
2223
deployment by using the {+driver-short+}.
2324

24-
.. _csharp_connection_uri:
25+
Overview
26+
--------
27+
28+
To connect to a MongoDB deployment, you need two things:
29+
30+
- A **connection URI**, also known as a *connection string*, which tells the {+driver-short+}
31+
which MongoDB deployment to connect to.
32+
- A **MongoClient** object, which creates the connection to the MongoDB deployment
33+
and lets you perform operations on it.
34+
35+
You can also use either of these components to customize the way {+driver-short+} behaves
36+
while connected to MongoDB.
37+
38+
This guide shows you how to create a connection URI and use a ``MongoClient`` object
39+
to connect to MongoDB.
2540

2641
Connection URI
2742
--------------
2843

29-
A **connection URI**, also known as a *connection string*, tells the driver how to connect to a MongoDB deployment and how to behave while connected.
30-
31-
A standard connection string includes the following pieces:
44+
A standard connection URI includes the following components:
3245

3346
.. list-table::
3447
:widths: 20 80
3548
:header-rows: 1
3649

37-
* - Piece
50+
* - Component
3851
- Description
3952

4053
* - ``mongodb://``
4154

4255
- Required. A prefix that identifies this as a string in the
4356
standard connection format.
4457

45-
* - ``username:password@``
58+
* - ``username:password``
4659

47-
- Optional. Authentication credentials. If you include these, the client will authenticate the user against the database specified in ``authSource``.
60+
- Optional. Authentication credentials. If you include these, the client
61+
authenticates the user against the database specified in ``authSource``.
62+
For more information about authentication settings, see
63+
:ref:`csharp-authentication-mechanisms`.
4864

4965
* - ``host[:port]``
5066

51-
- Required. The host and optional port number where MongoDB is running. If you don't include the port number, the driver will use the default port, ``27017``.
67+
- Required. The host and optional port number where MongoDB is running. If you don't
68+
include the port number, the driver uses the default port, ``27017``.
5269

5370
* - ``/defaultauthdb``
5471

5572
- Optional. The authentication database to use if the
5673
connection string includes ``username:password@``
5774
authentication credentials but not the ``authSource`` option. If you don't include
58-
this piece, the client will authenticate the user against the ``admin`` database.
75+
this component, the client authenticates the user against the ``admin`` database.
5976

6077
* - ``?<options>``
6178

@@ -64,94 +81,44 @@ A standard connection string includes the following pieces:
6481
:ref:`csharp-connection-options` for a full description of
6582
these options.
6683

67-
To use a connection URI, pass it as a string to the ``MongoClient`` constructor. In the
68-
following example, the driver uses a sample connection URI to connect to a MongoDB
69-
instance on port ``27017`` of ``localhost``:
70-
71-
.. literalinclude:: /includes/fundamentals/code-examples/connection/LocalConnection.cs
72-
:language: csharp
73-
:start-after: // start local connection
74-
:end-before: // end local connection
75-
76-
.. tip:: Reuse Your Client
77-
78-
Because each ``MongoClient`` represents a pool of connections to the
79-
database, most applications require only a single instance of
80-
``MongoClient``, even across multiple requests. To learn more about
81-
how connection pools work in the driver, see the :ref:`FAQ page
82-
<csharp-faq-connection-pool>`.
83-
84-
See :manual:`the MongoDB Manual </reference/connection-string>` for more information about creating a connection string.
85-
86-
MongoClientSettings
87-
-------------------
88-
89-
You can use a ``MongoClientSettings`` object to configure the connection in code
90-
rather than in a connection URI. To use a ``MongoClientSettings`` object, create an
91-
instance of the class and pass it as an argument to the ``MongoClient`` constructor.
92-
93-
In the following example, the driver uses a ``MongoClientSettings`` object to connect to a
94-
MongoDB instance on port ``27017`` of ``localhost``:
95-
96-
.. literalinclude:: /includes/fundamentals/code-examples/connection/MongoClientSettings.cs
97-
:language: csharp
98-
:dedent:
99-
:start-after: // start mongo client settings
100-
:end-before: // end mongo client settings
101-
102-
Other Connection Targets
103-
------------------------
104-
105-
Connect to Atlas
106-
~~~~~~~~~~~~~~~~
107-
108-
To connect to a MongoDB deployment on Atlas, create a client. You can
109-
create a client that uses your connection string and other
110-
client options by passing a ``MongoClientSettings`` object to the ``MongoClient``
111-
constructor.
84+
For more information about creating a connection string, see
85+
:manual:`Connection Strings </reference/connection-string?tck=docs_driver_csharp>` in the
86+
MongoDB Server documentation.
11287

113-
To specify your connection URI, pass it to the ``FromConnectionString()``
114-
method, which returns a new ``MongoClientSettings`` instance. To specify any other
115-
client options, set the relevant fields of the ``MongoClientSettings`` object.
88+
MongoClient
89+
-----------
11690

117-
You can set the {+stable-api+} version as a client option to avoid
118-
breaking changes when you upgrade to a new server version. To
119-
learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page
120-
<csharp-stable-api>`.
91+
To create a connection to MongoDB, pass a connection URI to the
92+
``MongoClient`` constructor. In the following example, the driver uses a sample
93+
connection URI to connect to a MongoDB deployment running on port ``27017`` of ``localhost``:abbreviation:
12194

122-
The following code shows how you can specify the connection string and
123-
the {+stable-api+} client option when connecting to a MongoDB
124-
deployment and verify that the connection is successful:
95+
.. code-block:: csharp
12596

126-
.. literalinclude:: /includes/fundamentals/code-examples/connection/AtlasConnection.cs
127-
:language: csharp
128-
:start-after: // start atlas connection
129-
:end-before: // end atlas connection
97+
const string connectionUri = "mongodb://localhost:27017/";
98+
var client = new MongoClient(connectionUri);
13099

131-
.. tip::
100+
Configuring the MongoClient
101+
---------------------------
132102

133-
Follow the :atlas:`Atlas driver connection guide </driver-connection?tck=docs_driver_nodejs>`
134-
to retrieve your connection string.
103+
You can configure settings for the ``MongoClient`` object by passing a
104+
``MongoClientSettings`` object to the constructor. The following example creates a
105+
``MongoClient`` object and sets the ``UseTls`` property to ``true``:
135106

136-
Connect to a Replica Set
137-
~~~~~~~~~~~~~~~~~~~~~~~~
107+
.. code-block:: csharp
138108

139-
To connect to a replica set deployment, specify the hostnames (or IP addresses) and
140-
port numbers of the members of the replica set.
109+
var mongoClientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost:27017/");
110+
mongoClientSettings.UseTls = true;
111+
var client = new MongoClient(mongoClientSettings);
141112

142-
If you aren't able to provide a full list of hosts in the replica set, you can
143-
specify one or more of the hosts in the replica set and instruct the driver to
144-
perform automatic discovery in one of the following ways:
113+
For a full list of the settings you can configure, see the
114+
`MongoClientSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClientSettings.html>`_
115+
API documentation.
145116

146-
- Specify the name of the replica set as the value of the ``replicaSet`` parameter.
147-
- Specify ``false`` as the value of the ``directConnection`` parameter.
148-
- Specify more than one host in the replica set.
117+
API Documentation
118+
-----------------
149119

150-
In the following example, the driver uses a sample connection URI to connect to the
151-
MongoDB replica set ``sampleRS``, which is running on port ``27017`` of three different
152-
hosts, including ``sample.host1``:
120+
To learn more about creating a ``MongoClient`` object with the {+driver-short+},
121+
see the following API documentation:
153122

154-
.. literalinclude:: /includes/fundamentals/code-examples/connection/ReplicaSetConnection.cs
155-
:language: csharp
156-
:start-after: // start replica set connection
157-
:end-before: // end replica set connection
123+
- `MongoClient <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClient.html>`__
124+
- `MongoClientSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClientSettings.html>`__

0 commit comments

Comments
 (0)