Users

Create users with MongoDB's commands and change or drop the ones that already exist.

Create users with MongoDB's commands and change or drop the ones that already exist. Create one per application with only the roles it needs.

Requirements

  • A user with userAdmin on the database or userAdminAnyDatabase.

Create

JavaScript
db.getSiblingDB("admin").createUser({ user: "reports", pwd: passwordPrompt(), roles: [{ role: "read", db: "shop" }] });

Change the password or the roles

JavaScript
db.getSiblingDB("admin").updateUser("reports", { pwd: passwordPrompt() });
db.getSiblingDB("admin").grantRolesToUser("reports", [{ role: "read", db: "invoices" }]);

Drop

db.getSiblingDB("admin").dropUser("reports");

Check

db.getSiblingDB("admin").runCommand({ usersInfo: "reports" }) shows the user with its roles, so after you create it, change its password or give it read on invoices you can see with that command whether the change is there.

Common problems

  • A user command answers Unauthorized (13) because whoever runs it lacks userAdmin.

See also