Search code examples
mongodbmongodb-.net-driver

How to create a user in MongoDB


I'm using the latest version of the driver and MongoDB database 2.6 and I used to create users using the following code:

        MongoUser _user1 = new MongoUser("username", "password", false);

        MongoDatabase.AddUser(_user1);

and now it is saying MongoDatabase.AddUser() is deprecated by showing the following information:

...is obsolete: Use the new user management command 'createUser' or 'updateUser'."

Where is this new user management command? How do I create a user using the new MongoDB C# driver?


Solution

  • I did a search for 'createUser' on the latest code on the GitHub repository: https://github.com/mongodb/mongo-csharp-driver/search?q=createUser&ref=cmdform

    At the time of writing, the only reference to 'createUser' is here

    The method has been marked as obsolete however in this commit

    However; on closer inspection of the code, I see this:

    if (_server.RequestConnection.ServerInstance.Supports(FeatureId.UserManagementCommands))
        {
            AddUserWithUserManagementCommands(user);
        }
        else
        {
           AddUserWithInsert(user);
        }
    

    So, it is calling the required method under the hood