Skip to content

Commit c7a4270

Browse files
Connection Pages (#52)
* connection * connection includes * BD feedback * fix links * autobuilder * wip * DL feedback * BD feedback * fix list * formatting * formatting * formatting * formatting * formatting * formatting * formatting * formatting * formatting * formatting Co-authored-by: Mike Woofter <[email protected]>
1 parent 483e70d commit c7a4270

File tree

13 files changed

+873
-3
lines changed

13 files changed

+873
-3
lines changed

snooty.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ version = "v{+version-number+}"
3030
example = "https://raw.githubusercontent.com/mongodb/docs-csharp/{+docs-branch+}/source/includes/usage-examples/code-examples"
3131
stable-api = "Stable API"
3232
api-root = "https://mongodb.github.io/mongo-csharp-driver/{+version-number+}/apidocs/html"
33-
bool-data-type = "``bool``"
34-
string-data-type = "``str``"
35-
int-data-type = "``int``"
33+
bool-data-type = "``boolean``"
34+
string-data-type = "``string``"
35+
int-data-type = "``integer``"
3636
not-available = "N/A"

source/fundamentals/connection.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.. _csharp-connection:
2+
3+
==========
4+
Connection
5+
==========
6+
7+
.. default-domain:: mongodb
8+
9+
.. toctree::
10+
11+
/fundamentals/connection/connect
12+
/fundamentals/connection/connection-options
13+
/fundamentals/connection/tls
14+
15+
.. contents:: On this page
16+
:local:
17+
:backlinks: none
18+
:depth: 1
19+
:class: singlecol
20+
21+
Overview
22+
--------
23+
24+
In this section, you'll learn how to use the {+driver-short+} to connect your application to a MongoDB deployment. Click a link in the following list to jump to a topic:
25+
26+
- :ref:`How to Connect to MongoDB <csharp-connect-to-mongodb>`
27+
- :ref:`Connection Options <csharp-connection-options>`
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
.. _csharp-connect-to-mongodb:
2+
3+
================
4+
Connection Guide
5+
================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
This guide shows you how to connect to a MongoDB instance or replica set
16+
deployment using the {+driver-short+}.
17+
18+
Connection URI
19+
--------------
20+
21+
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.
22+
23+
A standard connection string includes the following pieces:
24+
25+
.. list-table::
26+
:widths: 20 80
27+
:header-rows: 1
28+
29+
* - Piece
30+
- Description
31+
32+
* - ``mongodb://``
33+
34+
- Required. A prefix that identifies this as a string in the
35+
standard connection format.
36+
37+
* - ``username:password@``
38+
39+
- Optional. Authentication credentials. If you include these, the client will authenticate the user against the database specified in ``authSource``.
40+
41+
* - ``host[:port]``
42+
43+
- 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``.
44+
45+
* - ``/defaultauthdb``
46+
47+
- Optional. The authentication database to use if the
48+
connection string includes ``username:password@``
49+
authentication credentials but not the ``authSource`` option. If you don't include
50+
this piece, the client will authenticate the user against the ``admin`` database.
51+
52+
* - ``?<options>``
53+
54+
- Optional. A query string that specifies connection-specific
55+
options as ``<name>=<value>`` pairs. See
56+
:ref:`csharp-connection-options` for a full description of
57+
these options.
58+
59+
To use a connection URI, pass it as a string to the ``MongoClient`` constructor. In the
60+
following example, the driver uses a sample connection URI to connect to a MongoDB
61+
instance on port ``27017`` of ``localhost``:
62+
63+
.. literalinclude:: /includes/fundamentals/code-examples/connection/LocalConnection.cs
64+
:language: csharp
65+
66+
.. tip::
67+
68+
See :manual:`the MongoDB Manual</reference/connection-string>`: for more information about creating a connection string.
69+
70+
MongoClientSettings
71+
-------------------
72+
73+
You can use a ``MongoClientSettings`` object to configure the connection in code
74+
rather than in a connection URI. To use a ``MongoClientSettings`` object, create an
75+
instance of the class and pass it as an argument to the ``MongoClient`` constructor.
76+
77+
In the following example, the driver uses a ``MongoClientSettings`` object to connect to a
78+
MongoDB instance on port ``27017`` of ``localhost``:
79+
80+
.. literalinclude:: /includes/fundamentals/code-examples/connection/MongoClientSettings.cs
81+
:language: csharp
82+
:dedent:
83+
84+
Other Connection Targets
85+
------------------------
86+
87+
Connect to Atlas
88+
~~~~~~~~~~~~~~~~
89+
90+
In the following example, the driver uses a sample connection URI to connect to a MongoDB instance on Atlas with the credentials ``user1`` and ``password1``:
91+
92+
.. literalinclude:: /includes/fundamentals/code-examples/connection/AtlasConnection.cs
93+
:language: csharp
94+
95+
.. tip::
96+
97+
If your deployment is hosted on Atlas, follow the
98+
:atlas:`Atlas driver connection guide </driver-connection?tck=docs_driver_nodejs>`
99+
to retrieve your connection string.
100+
101+
Connect to a Replica Set
102+
~~~~~~~~~~~~~~~~~~~~~~~~
103+
104+
To connect to a replica set deployment, specify the hostnames (or IP addresses) and
105+
port numbers of the members of the replica set.
106+
107+
If you aren't able to provide a full list of hosts in the replica set, you can
108+
specify one or more of the hosts in the replica set and instruct the driver to
109+
perform automatic discovery in one of the following ways:
110+
111+
- Specify the name of the replica set as the value of the ``replicaSet`` parameter.
112+
- Specify ``false`` as the value of the ``directConnection`` parameter.
113+
- Specify more than one host in the replica set.
114+
115+
In the following example, the driver uses a sample connection URI to connect to the
116+
MongoDB replica set ``sampleRS``, which is running on port ``27017`` of three different
117+
hosts, including ``sample.host1``:
118+
119+
.. literalinclude:: /includes/fundamentals/code-examples/connection/ReplicaSetConnection.cs
120+
:language: csharp

0 commit comments

Comments
 (0)