mongodbHow to create a user in MongoDB?
Creating a user in MongoDB is a simple process.
use admin
db.createUser({
user: "username",
pwd: "password",
roles: [{ role: "readWrite", db: "myDB" }]
})
The above example code creates a user with username and password, and assigns the user the readWrite role on the myDB database.
use admin
: This command switches to the admin database.db.createUser
: This command creates a new user.user: "username"
: This specifies the username for the new user.pwd: "password"
: This specifies the password for the new user.roles: [{ role: "readWrite", db: "myDB" }]
: This assigns the user the readWrite role on the myDB database.
Output example
Successfully added user: {
"user" : "username",
"roles" : [
{
"role" : "readWrite",
"db" : "myDB"
}
]
}
Helpful links
More of Mongodb
- How to check the version of MongoDB?
- How to use watch in MongoDB?
- How to use unwind in MongoDB?
- How to insert new document into MongoDB?
- How to perform a health check for MongoDB?
- How to use triggers in MongoDB?
- How to work with time series data in MongoDB?
- How to list MongoDB users?
- How to update an array element in MongoDB?
- How to update many documents in MongoDB?
See more codes...