📜  mongo grant role tu user (1)

📅  最后修改于: 2023-12-03 14:44:21.154000             🧑  作者: Mango

Mongo Grant Role to User

Mongo Grant Role to User is a command that enables administrators to assign roles to users in a MongoDB database. With this command, administrators can grant users permissions to perform specific actions on the database. This command is particularly useful for organizations that have multiple users accessing MongoDB databases.

Syntax
db.grantRolesToUser(
   "<username>",
   [
      { role: "<role>", db: "<database>" },
      { role: "<role>", db: "<database>" }
   ]
)
Parameters

The Mongo Grant Role to User command takes the following parameters:

  • <username>: The username of the user to whom the roles are being assigned.

  • "<role>": The role being assigned to the user. This can be any built-in role, such as readWrite, read, dbAdmin, userAdmin, etc., or a custom role created by the administrator.

  • "<database>": The name of the database where the role is being assigned.

Example

The following example assigns the readWrite role to the user johndoe on the database mydb:

> use mydb

> db.grantRolesToUser(
...    "johndoe",
...    [
...       { role: "readWrite", db: "mydb" }
...    ]
... )
Conclusion

Mongo Grant Role to User is a powerful command that enables administrators to grant roles to users and provide them with specific permissions on a MongoDB database. This command can help organizations manage their databases more efficiently and effectively, while also ensuring the security and integrity of their data.