Introduction
Role-based access control helps keep data safe by giving users only the permissions they need.
Role-based access control helps keep data safe by giving users only the permissions they need.
db.createRole({
role: "roleName",
privileges: [
{
resource: { db: "databaseName", collection: "collectionName" },
actions: ["action1", "action2"]
}
],
roles: [{ role: "otherRole", db: "admin" }]
})db.createRole() to make a new role with specific permissions.privileges array defines what actions the role can do on which resources.db.createRole({
role: "readOnly",
privileges: [
{
resource: { db: "shop", collection: "products" },
actions: ["find"]
}
],
roles: []
})db.createRole({
role: "productManager",
privileges: [
{
resource: { db: "shop", collection: "products" },
actions: ["find", "insert", "update", "remove"]
}
],
roles: []
})db.createRole({
role: "admin",
privileges: [],
roles: [{ role: "readWrite", db: "admin" }, { role: "dbAdmin", db: "admin" }]
})This example creates a role that allows reading reports, assigns it to user 'alice', and then checks her roles.
db.createRole({
role: "reportViewer",
privileges: [
{
resource: { db: "sales", collection: "reports" },
actions: ["find"]
}
],
roles: []
})
// Assign the role to a user
use sales
db.grantRolesToUser("alice", [{ role: "reportViewer", db: "sales" }])
// Check user roles
db.getUser("alice")Always assign the least permissions needed to keep data safe.
Roles can include other roles to combine permissions easily.
Use db.getUser() to check what roles a user has.
Role-based access control lets you manage who can do what in your database.
Create roles with specific permissions using db.createRole().
Assign roles to users to control their access safely and clearly.
readOnlyUser with read access on the sales database?name, not role.find action on the database with empty collection string for all collections.db.runCommand({usersInfo: "alice"}) after assigning the role?db.createRole({
name: "reportViewer",
privileges: [{resource: {db: "reports", collection: ""}, actions: ["find"]}],
roles: []
})
db.createUser({
user: "alice",
pwd: "password123",
roles: ["reportViewer"]
})db.createRole({
role: "dataEditor",
privileges: [{resource: {db: "inventory", collection: "products"}, actions: ["update"]}],
roles: []
})manager that can read and update documents in the employees database and also inherit all privileges from the built-in read role on the same database. Which command correctly achieves this?Pass the test and this pattern moves unknown