MongoDB connection
Connection strings
To access a MongoDB instance, you need a connection string. Depending on your MongoDB deployment which can be either a Standalone, a Replica Set or a Shared Cluster, the connection string must be set accordingly.
The generic format for a MongoDB connection string is:
Standalone deployment
For a local standalone instance that doesn't require users to identify themselves, use the following connection string:
If access control is enabled then the connection string should contain the user's username and password and optionally the database associated with the user's credentials. The latter can be set using the authSource options parameter.
The above connection string will try to authenticate user chsakell with password myPassword against the persons database.
MongoClient
The easiest way to connect to a MongoDB standalone instance via C# is to create an instance of MongoClient
and pass the connection string to it's constructor:
Last updated