Overview

Users and roles in Mongo are per databases. Meaning if you want to create a user in a certain database, you have to use that database as follows:

User

User can be created as follows:

This command will create user called test in database hunter_dev(it will be authenticated by this database) and owner of the database. You can select all users from the database and collection: system.users as follows:

Or if you know the username you can also user:

Other way to see all the users authenticated to a certain database is to use the following command: Show users as follows:

Roles

Mongo has also some system roles. In nutshell, a user can be owned of a database, have readwrite accesses and have access to all databases. Roles are set during user creation:

Setting a role in the admin database, usually carries higher priority and rights and setting role in other databases. More information about roles you can check below:

Superuser Roles

Several roles provide either indirect or direct system-wide superuser access.

The following roles provide the ability to assign any user any privilege on any database, which means that users with one of these roles can assign themselves any privilege on any database:

dbOwner role, when scoped to the admin database

userAdmin role, when scoped to the admin database

userAdminAnyDatabase role