Intermediate LevelMongoDB & Mongoose
How does MongoDB Role-Based Access Control (RBAC) enforce database security?
❓ Interview Prompt: Explain Role-Based Access Control (RBAC), built-in roles (read, readWrite, dbAdmin, userAdmin), custom roles, and privilege actions.
MongoDB uses Role-Based Access Control (RBAC) to govern user access. Privileges consist of specified Actions granted on specific Resources:
Built-in Roles:
•
read / readWrite: Data access on target database.
•
dbAdmin: Index creation, collection stats, and validation.
•
userAdmin: Managing database user accounts.
Code Implementation Example
typescript
import mongoose from 'mongoose';
async function createApplicationUser() {
const db = mongoose.connection.db;
// Create readWrite user for application service
await db!.command({
createUser: 'app_service',
pwd: 'SecurePassword123!',
roles: [{ role: 'readWrite', db: 'ecommerce' }]
});
}💡Creating a database user with restricted readWrite role permissions.
🎯 Interview Key Takeaway
Enforce principle of least privilege by creating dedicated application users restricted to readWrite roles on target databases.
#RBAC#Security#Roles#Privileges#Intermediate
Back to all MongoDB & Mongoose Questions→