Categories: MongoDBNoSQL

MongoDB – How to Create Database Users

First and foremost, one would be required to enable authentication with MongoDB database. The details can be found on this page

MongoDB Database – Create User Command

Following steps can be used to create a database user:

  • Switch to the desired database. This is same as creating a database.
use databaseName
  • Create user using following command:
db.createUser({
    "user": "userName",
    "pwd": "somePassword",
    "roles": ["dbOwner"],
});

Above command would create user with with role such as dbOwner. The details on creating users can be found on this page covering the description on creating users – db.createUser() The user with role such as dbOwner can perform any administrative action on the database. The role combines the privileges granted by the readWrite, dbAdmin and userAdmin roles.

MongoDB – Built-in User Roles

Following are some of the roles which can be associated with the user:

  • Database user roles: Every database includes following client roles:
    • read
    • readWrite
  • Database administration roles: Every database includes following administration roles:
    • dbAdmin
    • dbOwner
    • userAdmin
  • Cluster administration roles: The admin database includes following roles for managing database cluster rather than the single database.
    • clusterAdmin
    • clusterManager
    • clusterMonitor
    • hostManager
  • Backup and restoration roles: The admin database includes following roles for backing up and restoring the data.
    • backup
    • restore
  • All-Database roles: Following roles are provided by admin database which applies to all the database except local and config databases.
    • readAnyDatabase
    • readWriteAnyDatabase
    • userAdminAnyDatabase
    • dbAdminAnyDatabase
  • Superuser roles: Provide full privileges on all resources.
    • root

The details on above roles can be found on this page, MongoDB Built-in User Roles.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar
Tags: mongodbnosql

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

2 months ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

2 months ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

3 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

3 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

3 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

3 months ago