如何在MongoDB中创建用户 [英] How to create a user in MongoDB

查看:73
本文介绍了如何在MongoDB中创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用驱动程序和MongoDB数据库2.6的最新版本,并且我曾经使用以下代码创建用户:

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);

,现在通过显示以下信息来表示不赞成使用MongoDatabase.AddUser():

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

...已过时:使用新的用户管理命令'createUser'或 'updateUser'."

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

这个新的用户管理命令在哪里?如何使用新的MongoDB C#驱动程序创建用户?

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

推荐答案

我在GitHub存储库上的最新代码中搜索了"createUser": https://github.com/mongodb/mongo-csharp -driver/search?q = createUser& ref = cmdform

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

在撰写本文时,仅引用'createUser'在这里

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

这篇关于如何在MongoDB中创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆