What are the best practices for managing MongoDB users and roles?
Instruction: Describe the best practices for creating and managing users and roles in MongoDB.
Updated
Official answer
Start with an access model: which human or application needs which actions on which databases or collections? Use distinct identities for applications and administrators, separate environments, and grant the minimum required roles. A user that only queries a collection should not inherit broad write or user-administration privileges.
Use built-in roles when their complete privileges fit the task; otherwise define a custom role with explicit resources and privilege actions such as find, insert or update. Inspect inherited privileges as well as directly granted ones. Treat user-management permissions as highly privileged: userAdmin on the admin database and userAdminAnyDatabase can let the holder grant themselves broader access.
Choose authentication supported by the actual edition, deployment and server version, such as SCRAM or X.509 where appropriate. Keep credentials out of source code and command history, manage their lifecycle, and require TLS with certificate validation. LDAP authentication and authorization have been deprecated since MongoDB 8.0; do not present LDAP as an unqualified new-deployment recommendation. Review the supported migration options for the deployment.
Audit access grants, removals and relevant authentication/authorization activity. Database auditing is an Enterprise feature for self-managed deployments and is available on supported Atlas tiers; it is not an automatic record of every operation on every MongoDB installation. Explicitly configure the supported audit events and filters, verify the events you need are captured, and protect and retain the resulting logs. Ordinary diagnostic logs are not a substitute for a tested audit policy.
Review effective access periodically and when a person's role or an application's needs change. Remove unused identities, revoke obsolete privileges and rotate credentials under a controlled process. Verify both an allowed operation and a denied operation with representative test identities. For Atlas, distinguish access to the Atlas management platform from database-user access. Apply supported security updates and test the access policy after changes; patching alone does not correct excessive privileges.
Sources
MongoDB: role-based access control; built-in roles; auditing; LDAP deprecation.