How to Manage Users

While MongoDB supports many user management commands, the driver does not have any helpers for them because users are generally managed from the MongoDB shell. However, it is still possible to manage users from the driver by using the IMongoDatabase.RunCommandAsync method.

Listing Users

The usersInfo command will provide information about users in a specific database.

// get the database
var db = client.GetDatabase("products");

// construct the usersInfo command
var command = new BsonDocument("usersInfo", 1);

// Run the command. If it fails, an exception will be thrown.
var result = await db.RunCommandAsync<BsonDocument>(command);

The result variable will contain a field called “users” which will contain all the users for the “products” database.

Creating Users

The following example uses the createUser command to add a user to a dataase.

// get the database
var db = client.GetDatabase("products");

// Construct the write concern
var writeConcern = WriteConcern.WMajority
    .With(wTimeout: TimeSpan.FromMilliseconds(5000);

// Construct the createUser command.
var command = new BsonDocument
{
    { "createUser", "accountAdmin01" },
    { "pwd", "cleartext password" },
    { "customData", new BsonDocument("employeeId", 12345) },
    { "roles", new BsonArray
               {
                   new BsonDocument
                   {
                       { "role", "clusterAdmin" },
                       { "db", "admin" }   
                   },
                   new BsonDocument
                   {
                       { "role", "readAnyDatabase" },
                       { "db", "admin" }   
                   },
                   "readWrite"
               }},
    { "writeConcern", writeConcern.ToBsonDocument() }
};

// Run the command. If it fails, an exception will be thrown.
await db.RunCommandAsync<BsonDocument>(command);

Updating Users

The following example uses the updateUser command to update a user in a database.

// get the database
var db = client.GetDatabase("products");

// Construct the updateUser command.
var command = new BsonDocument
{
    { "updateUser", "appClient01" },
    { "customData", new BsonDocument("employeeId", "0x3039") },
    { "roles", new BsonArray
               {
                   new BsonDocument
                   {
                       { "role", "read" },
                       { "db", "assets" }   
                   },
               }}
};

// Run the command. If it fails, an exception will be thrown.
await db.RunCommandAsync<BsonDocument>(command);

Dropping Users

The following example uses the dropUser command to drop a user from a database.

// get the database
var db = client.GetDatabase("products");

// Construct the dropUser command.
var command = @"{ dropUser: ""accountAdmin01"",
                  writeConcern: { w: ""majority"", wtimeout: 5000 }
                }";

// Run the command. If it fails, an exception will be thrown.
await db.RunCommandAsync<BsonDocument>(command);

Note
Even though we used a string here for the command, it could have been a BsonDocument like the other examples. Well-formed strings of valid JSON are interchangeable with BsonDocument.

Other User Management Commands

There are a number of other commands that exist for managing users and each would be run in a similar fashion to how ones demonstrated above.