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 use watch in MongoDB?
- What is MongoDB default port?
- How to perform a health check for MongoDB?
- How to list all indexes in MongoDB?
- How to use eq in MongoDB?
- How to update one document in MongoDB?
- How to order query results in MongoDB?
- How to check if array is empty in MongoDB?
- How to query with "not equal" condition in MongoDB?
- How to use transactions in MongoDB?
See more codes...