Skip to content

Commit aa9b341

Browse files
authored
DOCSP 24538 - C# Connection Guide (#6)
* add more constants * remove diagrams * add detail about connection string and targets
1 parent c880c91 commit aa9b341

File tree

8 files changed

+148
-5
lines changed

8 files changed

+148
-5
lines changed

snooty.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ driver-long = "MongoDB .NET/C# Driver"
1818
driver-short = ".NET/C# Driver"
1919
lang-framework = ".NET/C#"
2020
language = "C#"
21-
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
22-
version = "v2.17" # always set this to the driver branch (i.e. v1.7.0, v1.8.0, etc.)
23-
example = "https://raw.githubusercontent.com/mongodb/docs-csharp/{+docs-branch+}/source/includes/code-examples/"
21+
docs-branch = "master" # always set this to the docs branch (i.e. master, 1.7, 1.8, etc.)
22+
version-number = "2.17" # always set this to the driver branch (i.e. 1.7.0, 1.8.0, etc.)
23+
version = "v{+version-number+}"
24+
25+
example = "https://raw.githubusercontent.com/mongodb/docs-csharp/{+docs-branch+}/source/includes/usage-examples/code-snippets"
2426
stable-api = "Stable API"
25-
api = "https://mongodb.github.io/mongo-csharp-driver/{+version+}/apidocs"
27+
api-root = "https://mongodb.github.io/mongo-csharp-driver/{+version-number+}/apidocs/html"
Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,110 @@
1-
.. _csharp-connect-to-mongodb:
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 this piece, the client will authenticate the user against ``admin``.
50+
51+
* - ``?<options>``
52+
53+
- Optional. A query string that specifies connection-specific
54+
options as ``<name>=<value>`` pairs. See
55+
:ref:`csharp-connection-options` for a full description of
56+
these options.
57+
58+
.. tip::
59+
60+
See :manual:`the MongoDB Manual</reference/connection-string>`: for more information about creating a connection string.
61+
62+
Connection Targets
63+
------------------
64+
65+
Connect to a Standalone Deployment
66+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67+
68+
In the following example, the driver uses a sample connection URI to connect to a MongoDB instance on port ``27017`` of ``sample.host`` with the credentials ``user1`` and ``password1``:
69+
70+
.. literalinclude:: /includes/fundamentals/code-examples/StandaloneConnection.cs
71+
:language: csharp
72+
73+
Connect to Atlas
74+
~~~~~~~~~~~~~~~~
75+
76+
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``:
77+
78+
.. literalinclude:: /includes/fundamentals/code-examples/AtlasConnection.cs
79+
:language: csharp
80+
81+
.. tip::
82+
83+
If your deployment is hosted on Atlas, follow the
84+
:atlas:`Atlas driver connection guide </driver-connection?tck=docs_driver_nodejs>`
85+
to retrieve your connection string.
86+
87+
Connect to Your Local Machine
88+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
In the following example, the driver uses a sample connection URI to connect to a MongoDB instance on port ``27017`` of ``localhost``:
91+
92+
.. literalinclude:: /includes/fundamentals/code-examples/LocalConnection.cs
93+
:language: csharp
94+
95+
Connect to a Replica Set
96+
~~~~~~~~~~~~~~~~~~~~~~~~
97+
98+
In the following example, the driver uses a sample connection URI to connect to the MongoDB replica set ``sampleRS``, which is running on port ``27017`` of ``sample.host1``, ``sample.host2``, and ``sample.host3``:
99+
100+
.. literalinclude:: /includes/fundamentals/code-examples/ReplicaSetConnection.cs
101+
:language: csharp
102+
103+
104+
105+
106+
107+
108+
109+
110+
Binary file not shown.
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MongoDB.Driver;
2+
3+
// Connection URI
4+
const string connectionUri = "mongodb://localhost:27017";
5+
6+
// Create a new client and connect to the server
7+
var client = new MongoClient(connectionUri);
8+
Console.ReadKey();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MongoDB.Driver;
2+
3+
// Connection URI
4+
const string connectionUri = "mongodb://localhost:27017";
5+
6+
// Create a new client and connect to the server
7+
var client = new MongoClient(connectionUri);
8+
Console.ReadKey();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MongoDB.Driver;
2+
3+
// Connection URI
4+
const string connectionUri = "mongodb://sample.host1:27017,sample.host2:27017,sample.host3:27017/?replicaSet=sampleRS";
5+
6+
// Create a new client and connect to the server
7+
var client = new MongoClient(connectionUri);
8+
Console.ReadKey();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MongoDB.Driver;
2+
3+
// Connection URI
4+
const string connectionUri = "mongodb://user1:[email protected]:27017";
5+
6+
// Create a new client and connect to the server
7+
var client = new MongoClient(connectionUri);
8+
Console.ReadKey();

0 commit comments

Comments
 (0)