为此,请使用userAdminAnyDatabase,因为它会授予所有权限,例如admin。以下是语法-
use admin db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "yourRoleName", db: "yourDatabaseName" } ] } )
让我们实现以上语法以创建用户角色。以下是创建具有角色“ userAdminAnyDatabase”的用户的查询-
> use admin switched to db admin > db.createUser( ... { ... user: "David Miller", ... pwd: "123456", ... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] ... } ... )
这将产生以下输出-
Successfully added user: { "user" : "David Miller", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }