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);
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);
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.
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.
MongoClientSettings Property | Description | |||
|---|---|---|---|---|
AllowInsecureTls | Specifies whether to relax TLS constraints as much as possible. This can include | |||
ApplicationName | The app name the driver passes to the server in the client metadata as part of | |||
AutoEncryptionOptions | Settings for automatic client-side encryption. | |||
ClusterConfigurator | Low-level configuration options for sockets, TLS, cluster, and others. | |||
Compressors | The preferred compression types, in order, for wire-protocol messages sent to | |||
ConnectTimeout | The length of time the driver tries to establish a single TCP socket connection | |||
Credential | Settings for how the driver authenticates to the server. This includes | |||
DirectConnection | Specifies whether to force dispatch all operations to the host. | |||
HeartbeatInterval | The interval between regular server-monitoring checks. Must be greater than or | |||
HeartbeatTimeout | The length of time a monitoring socket can be idle before timing out. | |||
IPv6 | Specifies whether the host address is in IPv6 format. | |||
IsFrozen | Indicates whether the settings have been frozen. Frozen settings can't be changed. | |||
LinqProvider | The LINQ provider to use. | |||
LoadBalanced | Specifies whether the driver is connecting to a load balancer. You can set this
Data Type: | |||
LocalThreshold | The latency window for server eligibility. If a server's round trip takes longer | |||
LoggingSettings | The settings used for logging. | |||
MaxConnecting | The greatest number of connections a driver's connection pool may be | |||
MaxConnectionIdleTime | The length of time a connection can be idle before the driver closes it. | |||
MaxConnectionLifeTime | The length of time a connection can be pooled before expiring. | |||
MaxConnectionPoolSize | The greatest number of clients or connections the driver can create in its | |||
MinConnectionPoolSize | The number of connections the driver should create and keep in the connection | |||
ReadConcern | The client's default read concern. | |||
ReadEncoding | The UTF-8 encoding to use for string deserialization. | |||
ReadPreference | The client's default read-preference settings. You can include the | |||
ReplicaSetName | The name of the replica set to connect to. | |||
RetryReads | Enables retryable reads. | |||
RetryWrites | Enables retryable writes. | |||
Scheme | Specifies whether to use the standard connection string format ( | |||
Server | The host and port number where MongoDB is running. | |||
ServerApi | Allows opting into Stable API versioning. See | |||
ServerMonitoringMode | Specifies the server monitoring protocol to use. When | |||
Servers | The cluster members where MongoDB is running. | |||
ServerSelectionTimeout | The length of time the driver tries to select a server before timing out. | |||
SocketTimeout | The length of time the driver tries to send or receive on a socket before | |||
SrvMaxHosts | The greatest number of SRV results to randomly select when initially populating | |||
SrvServiceName | The service name of the SRV resource records | |||
SslSettings | TLS/SSL options, including client certificates, revocation handling, and | |||
UseTls | Specifies whether to require TLS for connections to the server. If you use | |||
WaitQueueTimeout | The length of time the driver tries to check out a connection from a | |||
WriteConcern | The default write-concern settings, including write timeout and | |||
WriteEncoding | Specifies whether UTF-8 string serialization is strict or lenient. With strict |