Database Users and Roles — MongoDB Technical Interview Questions & Answers
Creating users and assigning role-based permissions.
Database Users and Roles
- Simple meaning: MongoDB users live in a database (often admin) and have roles like readWrite or read.
- How to use (local, after enabling auth): use admin then db.createUser({ user: "asha", pwd: "secret", roles: [{ role: "readWrite", db: "college" }] }).
- Atlas: Database Access UI creates users — easier than shell for students.
- Connect: mongodb://asha:secret@localhost:27017/college?authSource=admin
- Common mistake: creating a user but leaving mongod without --auth / security.authorization. User is ignored.
- Interview tip: User + role + authSource. Atlas UI for practice.
Technical Interview Tip: User, role, authSource. Atlas UI is fine to mention.
Code Examples for Database Users and Roles
Database Users and Roles — working example
// Database Users and Roles
db.users.find({ active: true }).sort({ name: 1 }).limit(5)Database Users and Roles: find active users, sort by name, limit 5.
How to Explain Database Users and Roles to an Interviewer
Where do you create Atlas users?
Atlas Database Access Roles like readWrite