This configuration was used in MongoDB 2.6 and may not be compatible with other versions.

There is a condition that when MongoDB is installed, it does not have an administrator user, this condition is called a localhost exception and allows you to create the first user in the database admin with userAdmin or userAdminAnyDatabase permissions.

The commands must be executed in the mongo Shell and to open it, type:

$ mongo

There are several parameters for the mongo command, which can be seen at this link.

With the commands below we create an admin user.

Note: Remembering that <username> and <password> must be changed to the username and password you want to create.

use admin
db.createUser(
  {
    user: "<user>",
    pwd: "<password>",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" },
             "readWriteAnyDatabase"]
  }
)

References