|
| 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