EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Users
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

MongoDB Users

By Priya PedamkarPriya Pedamkar

MongoDB Users

Definition of MongoDB User

MongoDB users are used to accessing the database as per specified grants that the user defined. The user is more important and useful in every database to access the database through the client. In MongoDB, we have to use the db.createUser method to create a new user; when adding any user into the MongoDB database, we can assign a role to the user to privileges. We can also update the user after creating, we can change the user name, password, and grant, revoke the privileges of the user. After adding a user into the database, we can create a user-specific database.

MongoDB User Operations and Roles

  • Access control on the database is more important when we are working with users and roles in MongoDB.
  • By default authorization to the database is disabled; we need to enable this using adding authorization in configuration files.
  • The below examples show the use of security options in MongoDB.

#Security

Authorization: enabled

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,471 ratings)

cat /etc/mongod.conf

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Output
Output
  • The authorization setting in MongoDB will enable and disable the role-based access control.
  • Below syntax shows that create a user in MongoDB, db.createUser method is used to create a new user in MongoDB.

Syntax:

db.createUser (user_info, Writeconcern)

Parameter:

Below is the parameter description syntax of creating a user in MongoDB.

  • Create User: Create user is a method that was used to create a new user in MongoDB. Using create user method, we have to create new users as per the operation specified in MongoDB.
  • User or user_info: Type of user parameter is a document in creating user method. This parameter will contain the document and authentication of the user. User info contains username and privileges, which we have defining for the user.
  • Write concern: This is an optional parameter to create a user method in MongoDB. Get last error command takes the same field as write concern in MongoDB.

MongoDB User and Role Operations

  • To create a role with authentication restrictions in MongoDB, we must have set authentication restrictions on the database which we have using.
1. Create Role in MongoDB to Manage Current Operations
  • In the following example, we have created a role name as user_test, which is used to run the only db.currentOp () and db.killOp () command.
  • For creating any user in MongoDB, we need to connect the mongodb instance.

Example:

use admin
db.createRole (
{
role: "user_test",
privileges: [
{ resource: { cluster: true }, actions: [ "killop", "inprog" ] },
{ resource: { db: "", collection: "" }, actions: [ "killCursors" ] }
],
roles: [] })

Output

  • To create a user in MongoDB, we need to use the admin database first and then create a user name as user_test.
2. Create User to Run Mongostat Command
  • In the below example, we have to create a role name as user_test1, which is used to provide the privileges that run only a command name as mongostat.

Example:

use admin
db.createRole(
{
role: "user_test1",
privileges: [
{ resource: { cluster: true }, actions: [ "serverStatus" ] }
],
roles: [] }
)

Output

3. Create User and Provide Privileges to Drop System Views in Collection Across Databases
  • In the below example, we have created a user name as user_test2 and provided permission to drop the system view collection.

Example:

use admin
db.createRole(
{
role: "user_test2",
privileges: [
{
actions: [ "dropCollection" ],
resource: { db: "", collection: "system.views" }
}
],
roles: [] }
)

MongoDB Users-1.4

4. Change user Password in MongoDB
  • We can change the password of the existing user by using the change user password method in MongoDB.
  • We have to change the user_test user password to “user”. We need to pass the user’s current username and password to change the user’s password in MongoDB.
  • We need to specify the user name when we want to change the user password in MongoDB.

use test_bak;
db.changeUserPassword ("user_test", "user")

MongoDB Users-1.5

  • To modify the password of any or existing user, we need to use the change user password method in MongoDB.
5. Modify the user access in MongoDB
  • We can modify the access of the existing or any user in MongoDB. We can give access and revoke the privileges of an existing user.
  • Below is the access which was available in MongoDB.
    • Revoke grants from the role.
    • Grant privileges to role.

1. Revoke a Grants from Role

  • We can revoke the role of the existing user by using the db.revokeRolesFromUser () method.
  • In the below example, we have revoked the grant of user_test user. We have revoked the read grants from the user_test user.

Example:

use test_bak
db.revokeRolesFromUser("user_test",
[ { role: "read", db: "test_bak" }
])

MongoDB Users-1.6
2. Grant Privileges to Role

  • We can provide grants to the role or existing user by using the db.grantRolesToUser () method.
  • In the below example, we have grantprivileges to the user_test user. We have provided readrole on test_bak database to user_test user.

Example:

use test_bak
db.grantRolesToUser(
"user_test",
[
{ role: "read", db: "test_bak" }
] )

MongoDB Users-1.7

6. View user role
  • We can check the user role by using the db.getUser method in MongoDB. We have to check all the details of the user_test user.
  • We have to use the database name before using the method of db.getUser in MongoDB.

Example:

use test_bak;
db.getUser("user_test")

MongoDB Users-1.8

7. Create User and Provide Read Write Grant to The User
  • In the below example, we have created a user name as user_test3 and can read and write on the database.
  • We have provided read and write roles on test_bak and test_bak1 databases.

Example:

db.createUser(
{
user: "uset_test3",
pwd: "password",
roles: [
{ role: "read", db: "test_bak" },
{ role: "read", db: "test_bak1" },
{ role: "readWrite", db: "test_bak" },
{ role: "readWrite", db: "test_bak1" }
] }
)

MongoDB Users-1.9

Recommended Articles

This is a guide to MongoDB Users. Here we also discuss the definition and MongoDB users operations and roles along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. MongoDB count
  2. MongoDB Geospatial
  3. MongoDB Delete
  4. MongoDB Array
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (14 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (7 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
2 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more