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.

Latest posts by Ajitesh Kumar (see all)
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

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

2 weeks ago

Agentic AI Design Patterns Examples

In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…

2 weeks ago

List of Agentic AI Resources, Papers, Courses

In this blog, I aim to provide a comprehensive list of valuable resources for learning…

2 weeks ago

Understanding FAR, FRR, and EER in Auth Systems

Have you ever wondered how systems determine whether to grant or deny access, and how…

3 weeks ago

Top 10 Gartner Technology Trends for 2025

What revolutionary technologies and industries will define the future of business in 2025? As we…

3 weeks ago

OpenAI GPT Models in 2024: What’s in it for Data Scientists

For data scientists and machine learning researchers, 2024 has been a landmark year in AI…

3 weeks ago