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. For latest updates and blogs, follow us on Twitter. 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. Check out my other blog, Revive-n-Thrive.com

Share
Published by
Ajitesh Kumar
Tags: mongodbnosql

Recent Posts

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

18 hours ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

24 hours ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

2 days ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

2 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

2 days ago

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

7 days ago