use admin
db.createUser({ user: 'user', pwd: 'password', roles: [ 'userAdminAnyDatabase' ] })
or
db.createUser({user:"username",pwd:"password", roles:[{role:"root",db:"admin"}]})
use dbname
db.createUser(
{
user: "username",
pwd: "passsowrd",
roles: ["readWrite"]
}
)
db.grantRolesToUser(
"username",
[
{ role: "readWrite", db: "dbname" }
]
)
"username",
[
{ role: "readWrite", db: "dbname" }
]
)