Connection Options
This section describes the MongoDB connection and authentication options
available in the .NET/C# Driver. You can configure your connection using either
the connection URI or a MongoClientSettings
object.
Using the Connection URI
If you pass a connection URI to the MongoClient
constructor, you can include
connection options in the string as <name>=<value>
pairs. In the following example,
the connection URI contains the connectTimeoutMS
option with a value of 60000
and the tls
option with a value of true
:
using MongoDB.Driver; // Sets the connection URI const string connectionUri = "mongodb+srv://sample.host:27017/?connectTimeoutMS=60000&tls=true"; // Creates a new client and connects to the server var client = new MongoClient(connectionUri);
To learn more about the options that you can specify in a connection URI, see /reference/connection-string-options in the MongoDB Server manual.
Using MongoClientSettings
You can use a MongoClientSettings
object to configure connection settings in code
rather than in a connection URI. Configuring the connection this way makes it easier to
change settings at runtime, helps you catch errors during compilation, and provides
more configuration options than the connection URI.
To use a MongoClientSettings
object, create an instance of the class, set
its properties, and pass it as an argument to the MongoClient
constructor:
//const string connectionUri = "mongodb+srv://sample.host:27017/?connectTimeoutMS=60000&tls=true"; // Creates a MongoClientSettings object var settings = new MongoClientSettings() { Scheme = ConnectionStringScheme.MongoDBPlusSrv, Server = new MongoServerAddress("sample.host", 27017), ConnectTimeout = new TimeSpan(0, 0, 60), UseTls = true }; // Creates a new client and connects to the server var client = new MongoClient(settings);
Using MongoUrlBuilder
You can use a MongoUrlBuilder
object to configure connection settings in code
rather than in a connection URI. Configuring the connection this way makes it easier to
change settings at runtime, helps you catch errors during compilation, and provides
more configuration options than the connection URI.
To use a MongoUrlBuilder
object, create an instance of the class, set
its properties, and pass it as an argument to the MongoClient
constructor:
//const string connectionUri = "mongodb+srv://sample.host:27017/?connectTimeoutMS=60000&tls=true"; // Creates a MongoClientSettings object var settings = new MongoClientSettings() { Scheme = ConnectionStringScheme.MongoDBPlusSrv, Server = new MongoServerAddress("sample.host", 27017), ConnectTimeout = new TimeSpan(0, 0, 60), UseTls = true }; // Creates a new client and connects to the server var client = new MongoClient(settings);
Tip
Other MongoUrl Members
The MongoUrl
class contains other methods and read-only properties that you can
use to retrieve information about your connection. For a full list, see
MongoUrl in the
API documentation.
Connection Options
The following table lists each connection option available in the
MongoClientSettings
class, and, if possible, how to achieve the same result in the
connection string. If a MongoClientSettings
property maps to more than one
option in the connection string, the Connection URI Example shows all
relevant options.
If you use your connection URI to create a MongoUrl
or ConnectionString
object,
you can use the object's read-only properties to access the values of the connection
options. These properties are listed in the MongoUrl
column of the table.
Note
If you're using a query parameter for a time duration, the value must be in
milliseconds. For example, to specify 60 seconds, use the value 60000
. If you're
using a MongoClientSettings
object for a time duration, use the appropriate
TimeSpan
value.
Replica Set Options
ReplicaSetName
The name of the replica set to connect to. The default value is null
.
var settings = new MongoClientSettings(); settings.ReplicaSetName = "yourReplicaSet";
var builder = new MongoUrlBuilder(); builder.ReplicaSetName = "yourReplicaSet";
DirectConnection
Specifies whether to force dispatch all operations to the host.
If you specify this option, the driver doesn't accept the
DNS seed list connection format.
You must use the standard connection URI format
instead. The default value is false
.
This property must be set to false
if you specify more than one
host name.
var settings = new MongoClientSettings(); settings.DirectConnection = true;
var builder = new MongoUrlBuilder(); builder.DirectConnection = true;
TLS Options
AllowInsecureTls
Specifies whether to relax TLS constraints as much as possible. This can include
allowing invalid certificates or hostname mismatches. The default value is false
.
If this property is set to true
and SslSettings.CheckCertificateRevocation
is set to false
, the .NET/C# Driver will throw an exception.
The following code example shows how to set this option to true
:
var settings = new MongoClientSettings(); settings.AllowInsecureTls = true;
AllowInsecureTls
Specifies whether to relax TLS constraints as much as possible. This can include
allowing invalid certificates or hostname mismatches. The default value is false
.
If this property is set to true
and SslSettings.CheckCertificateRevocation
is set to false
, the .NET/C# Driver will throw an exception.
The following code example shows how to set this option to true
:
var builder = new MongoUrlBuilder(); builder.AllowInsecureTls = true;
tlsInsecure
Specifies whether to relax TLS constraints as much as possible. This can include
allowing invalid certificates or hostname mismatches. The default value is false
.
If this property is set to true
and SslSettings.CheckCertificateRevocation
is set to false
, the .NET/C# Driver will throw an exception.
The following code example shows how to set this option to true
:
mongodb://localhost:27017/?tlsInsecure=true