Skip to content

Commit 1fe89e9

Browse files
authored
DOCSP-49053: Standardize MongoClient page (#621)
1 parent c233f4f commit 1fe89e9

File tree

1 file changed

+58
-35
lines changed

1 file changed

+58
-35
lines changed

source/connect/mongoclient.txt

Lines changed: 58 additions & 35 deletions
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 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+
Connecting to a MongoDB deployment requires the following components:
29+
30+
- **Connection URI**, also known as a *connection string*, which tells the {+driver-short+}
31+
which MongoDB deployment to connect to.
32+
- **MongoClient** object, which creates and sustains the connection to the MongoDB deployment
33+
and lets you perform data operations.
34+
35+
You can also specify connection settings in either of these components to customize the way
36+
the {+driver-short+} behaves 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,37 +81,43 @@ 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``:
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.
87+
88+
MongoClient
89+
-----------
90+
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``:
94+
95+
.. code-block:: csharp
96+
97+
const string uri = "mongodb://localhost:27017/";
98+
var client = new MongoClient(uri);
7099

71-
.. literalinclude:: /includes/fundamentals/code-examples/connection/LocalConnection.cs
72-
:language: csharp
73-
:start-after: // start local connection
74-
:end-before: // end local connection
100+
Configure the MongoClient
101+
-------------------------
75102

76-
.. tip:: Reuse Your Client
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``:
77106

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>`.
107+
.. code-block:: csharp
83108

84-
See :manual:`the MongoDB Manual </reference/connection-string>` for more information about creating a connection string.
109+
var connectionString = "mongodb://localhost:27017/"
110+
var settings = MongoClientSettings.FromConnectionString(connectionString);
111+
settings.UseTls = true;
85112

86-
MongoClientSettings
87-
-------------------
113+
To view a full list of the settings you can configure, see the
114+
:ref:`csharp-connection-options` guide.
88115

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.
116+
API Documentation
117+
-----------------
92118

93-
In the following example, the driver uses a ``MongoClientSettings`` object to connect to a
94-
MongoDB instance on port ``27017`` of ``localhost``:
119+
To learn more about creating a ``MongoClient`` object with the {+driver-short+},
120+
see the following API documentation:
95121

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
122+
- `MongoClient <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClient.html>`__
123+
- `MongoClientSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClientSettings.html>`__

0 commit comments

Comments
 (0)