Related Topics
Overview Of MongoDB
MongoDB Page 1
MongoDB Page 2
MongoDB Page 3
No SQl Database
MongoDB Page 4
MongoDB Page 5
Advantages Over RDBMS
MongoDB Page 6
MongoDB Page 7
MongoDB Data Types
MongoDB Page 8
MongoDB Data Modeling
MongoDB Page 9
Query & Projection Operator
MongoDB Page 10
MongoDB Page 11
MongoDB Update Operator
MongoDB Page 12
AggregationPipeline Stages
MongoDB Page 13
MongoDB Page 14
MongoDB Limit()
MongoDB Page 15
MongoDB Sort()
MongoDB Page 16
Query Modifiers
MongoDB Page 17
Aggregation Commands
MongoDB Page 18
Geospatial Command
MongoDB Page 19
Query and Write Operation Commands
MongoDB Page 20
Query Plan Cache Commands
MongoDB Page 21
Authentication Commands
MongoDB Page 22
Role Management Commands
MongoDB Page 23
Replication Command
MongoDB Page 24
Shading Commands
MongoDB Page 25
Session Commands
MongoDB Page 26
Create Database
MongoDB Page 27
Drop Database
MongoDB Page 28
Create Collection
MongoDB Page 29
Drop Collection
MongoDB Page 30
Inset Documents
MongoDB Page 31
Update Documents
MongoDB Page 32
Delete Documents
MongoDB Page 33
SQL to MongoDB Mapping
MongoDB Page 34
Introduction to React.js
React JS Page 1
React JS Page 2
React JS Page 3
Components in React.js
React JS Page 4
React JS Page 5
Virtual DOM in React.js
React JS Page 6
React JS Page 7
State and Props in React.js
React JS Page 8
React JS Page 9
React Router
React JS Page 10
React JS Page 11
React Hooks
React JS Page 12
React JS Page 13
Redux in React.js
React JS Page 14
React JS Page 15
Context API in React.js
React JS Page 16
React JS Page 17
React with Webpack and Babel
React JS Page 18
React JS Page 19
Testing in React.js
React JS Page 20
React JS Page 21
Deployment and Optimization in React.js
React JS Page 22
React JS Page 23
Emerging Trends and Best Practices in React.js
React JS Page 24
React JS Page 25
Introduction
Node.js Page 1
Node.js Page 2
Node.js Architecture and Event-Driven Programming
Node.js Page 3
Node.js Page 4
Modules and Packages in Node.js
Node.js Page 5
Node.js Page 6
File System and Buffers in Node.js
Node.js Page 7
Node.js Page 8
HTTP and Networking in Node.js
Node.js Page 9
Node.js Page 10
Express.js and Web Applications
Node.js Page 11
Node.js Page 12
Databases and ORMs in Node.js
Node.js Page 13
Node.js Page 14
RESTful APIs in Node.js
Node.js Page 15
Node.js Page 16
Testing and Debugging in Node.js
Node.js Page 17
Deployment and Scalability in Node.js
Node.js Page 18
Node.js Page 19
Emerging Trends and Best Practices in Node.js
Node.js Page 20
Node.js Page 21
Performance Optimization in Node.js
Node.js Page 22
Node.js Page 23

MongoDB
db.createUser({
user: "username",
pwd: "password",
roles: [
{ role: "roleName", db: "databaseName" },
// Additional roles if needed
]
})
Let’s go through an example:
db.createUser({
user: "johnDoe",
pwd: "mySecretPassword",
roles: [
{ role: "readWrite", db: "myDatabase" },
{ role: "dbAdmin", db: "myDatabase" }
]
})
In this example, we are creating a user named “johnDoe” with the password “mySecretPassword”. The user is assigned two roles: “readWrite” and “dbAdmin” for the “myDatabase” database. The “readWrite” role grants read and write access to the database, allowing the user to perform CRUD operations on the collections within “myDatabase”. The “dbAdmin” role provides administrative privileges for the “myDatabase”, allowing the user to manage the database, create indexes, and perform other administrative tasks.
After executing the createUser()
command, MongoDB will create the user account with the specified credentials and roles. The user can then authenticate using the provided username and password to access the database and perform actions based on the assigned roles.
It’s important to note that the createUser()
command should be executed with administrative privileges, such as a user with the “userAdminAnyDatabase” or “root” role, to create users and assign roles effectively.
Please keep in mind that the specific roles available may vary depending on the version of MongoDB you are using. It’s recommended to refer to the MongoDB documentation for the most up-to-date information on available roles and their privileges.
In summary, the createUser()
command in MongoDB allows you to create new user accounts, define their authentication credentials, and assign roles to control access and permissions for the database system.
db.updateUser("johnDoe", {
roles: [
{ role: "read", db: "myDatabase" },
{ role: "read", db: "anotherDatabase" }
]
})
In this example, we are modifying the user “johnDoe” and updating the roles associated with the account. The user will now have the “read” role for both the “myDatabase” and “anotherDatabase”. The db.updateUser()
method modifies the existing user account with the specified changes.
db.dropUser("username")
Let’s go through an example:
db.dropUser("johnDoe")
In this example, we are using the dropUser()
command to delete the user account named “johnDoe”. After executing the command, the user account will be removed from the database, and the associated credentials and roles will be permanently deleted.
It’s important to note that the dropUser()
command should be executed with administrative privileges, such as a user with the “userAdminAnyDatabase” or “root” role, to delete user accounts effectively.
Here are a few points to keep in mind about the dropUser()
command:
The command permanently deletes the user account and its associated credentials. Once a user account is dropped, it cannot be recovered.
If the user account being dropped is the same account currently being used to authenticate the current session, it may result in immediate loss of access to the database.
The
dropUser()
command affects only the user account within a specific database. To drop a user account across all databases, you would need to execute thedropUser()
command in each individual database.User accounts can only be dropped by users with appropriate administrative privileges.
When to use the dropUser()
command:
Use
dropUser()
when you want to permanently remove a user account from the database system.You may use it when a user no longer requires access to the database or when their role or privileges need to be completely revoked.
Please be cautious while using the dropUser()
command as it irreversibly deletes user accounts. Always ensure that you have a proper backup and follow appropriate security practices when managing user accounts in MongoDB.
db.grantRolesToUser("username", [ { role: "roleName", db: "databaseName" }, ... ])
Here’s an example of how to use the grantRolesToUser()
command:
db.grantRolesToUser("johnDoe", [
{ role: "readWrite", db: "myDatabase" },
{ role: "dbAdmin", db: "myDatabase" }
])
In this example, we are granting the “readWrite” and “dbAdmin” roles to the user account “johnDoe” for the “myDatabase” database. The user will have read and write access to the collections in “myDatabase” and administrative privileges to manage the database.
2. revokeRolesFromUser()
: The revokeRolesFromUser()
command is used to remove one or more roles from a user account in MongoDB. It allows you to revoke specific privileges and permissions from a user, restricting their access and capabilities within the database.
The basic syntax for the revokeRolesFromUser()
command is as follows:
db.revokeRolesFromUser("username", [ { role: "roleName", db: "databaseName" }, ... ])
Here’s an example of how to use the revokeRolesFromUser()
command:
db.revokeRolesFromUser("johnDoe", [
{ role: "readWrite", db: "myDatabase" },
{ role: "dbAdmin", db: "myDatabase" }
])
In this example, we are revoking the “readWrite” and “dbAdmin” roles from the user account “johnDoe” for the “myDatabase” database. The user will no longer have the read and write access or the administrative privileges in “myDatabase”.
It’s important to note that both commands should be executed with administrative privileges, such as a user with the “userAdminAnyDatabase” or “root” role, to manage user roles effectively.
When to use grantRolesToUser()
and revokeRolesFromUser()
:
Use
grantRolesToUser()
when you want to assign new roles and privileges to a user account.Use
revokeRolesFromUser()
when you want to remove existing roles and privileges from a user account.
These commands are particularly useful when you need to adjust the access and permissions for specific users within your MongoDB database, allowing you to manage user roles and their associated privileges dynamically.
use myDatabase
db.getUser("johnDoe")
In this example, we switch to the “myDatabase” database and retrieve information about the user account “johnDoe”. The method will return an object containing details such as the user’s roles, privileges, authentication mechanisms, and other attributes associated with the account.
The purpose of the db.getUser()
method is to fetch information about a specific user account. It can be helpful for administrators or developers who need to review or validate the properties and settings of a user account in MongoDB.




Popular Category
Topics for You
Introduction to React.js
React JS Page 1
React JS Page 2
React JS Page 3
Components in React.js
React JS Page 4
React JS Page 5
Virtual DOM in React.js
React JS Page 6
React JS Page 7
State and Props in React.js
React JS Page 8
React JS Page 9
React Router
React JS Page 10
React JS Page 11
React Hooks
React JS Page 12
React JS Page 13
Redux in React.js
React JS Page 14
React JS Page 15
Context API in React.js
React JS Page 16
React JS Page 17
React with Webpack and Babel
React JS Page 18
React JS Page 19
Testing in React.js
React JS Page 20
React JS Page 21
Deployment and Optimization in React.js
React JS Page 22
React JS Page 23
Emerging Trends and Best Practices in React.js
React JS Page 24
React JS Page 25
Introduction
Node.js Page 1
Node.js Page 2
Node.js Architecture and Event-Driven Programming
Node.js Page 3
Node.js Page 4
Modules and Packages in Node.js
Node.js Page 5
Node.js Page 6
File System and Buffers in Node.js
Node.js Page 7
Node.js Page 8
HTTP and Networking in Node.js
Node.js Page 9
Node.js Page 10
Express.js and Web Applications
Node.js Page 11
Node.js Page 12
Databases and ORMs in Node.js
Node.js Page 13
Node.js Page 14
RESTful APIs in Node.js
Node.js Page 15
Node.js Page 16
Testing and Debugging in Node.js
Node.js Page 17
Deployment and Scalability in Node.js
Node.js Page 18
Node.js Page 19
Emerging Trends and Best Practices in Node.js
Node.js Page 20
Node.js Page 21
Performance Optimization in Node.js
Node.js Page 22
Node.js Page 23
Go through our study material. Your Job is awaiting.