|
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 | + |
0 commit comments