Concept Flow - Built-in roles (read, readWrite, dbAdmin)
Start: User needs permissions
Choose built-in role
read
Access granted based on role
End
User selects one built-in role to get specific database permissions: read, readWrite, or dbAdmin.
use mydb
db.createUser({
user: "alice",
roles: ["readWrite"]
})| Step | Action | Role Assigned | Permissions Granted | Result |
|---|---|---|---|---|
| 1 | Switch to database 'mydb' | N/A | N/A | Context set to 'mydb' |
| 2 | Create user 'alice' with role 'readWrite' | readWrite | Read and write data on 'mydb' | User 'alice' created |
| 3 | User 'alice' tries to read data | readWrite | Allowed | Read operation successful |
| 4 | User 'alice' tries to write data | readWrite | Allowed | Write operation successful |
| 5 | User 'alice' tries to perform admin task | readWrite | Denied | Operation failed: insufficient privileges |
| 6 | End | N/A | N/A | User permissions enforced based on role |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| user | undefined | alice (readWrite) | alice (readWrite) | alice (readWrite) | alice (readWrite) |
| permissions | none | read and write | read allowed | read and write allowed | read and write allowed |
Built-in roles in MongoDB control user permissions. read: can only read data. readWrite: can read and write data. dbAdmin: can manage database structure. Assign roles when creating users to control access. Permissions are enforced automatically by MongoDB.