

Mongo :/admin script.js -u -authenticationDatabase "admin" -pĪ password prompt would appear after running the command above, you should enter the password of the admin user. Replace with the userName of the admin user You would run the script like this: // Replace and with the host and port values of your mongoDB server.

The password can also be written in clear text here Pwd: passwordPrompt(), // This would prompt a password for the new user. By creating a role with that are scoped to a specific collection in a particular database, administrators can. Administrators can implement collection-level access control through user-defined roles. Print('User does not exist, creating a user.') MongoDB Shell (mongosh) Collection-level access control allows administrators to grant users privileges that are scoped to specific collections. If the user does not exist, create the user and add roles Print('User already exist, granting roles.')
#MONGODB GRANT READWRITE UPDATE#
If the user already exists, update the user roles Change to the database expected to store the user detailsĬonst userDb = db.getSiblingDB(userDatabase) Get a list of all database matching the regExp /^example.*$/Ĭonst dbs = db.adminCommand()) Change the value of this variable to the name of the user to grant the roles Change the value of this variable to the name of the database expected to store the user details Use grantRolesToUser function to grant read & write roles.

For instructions on enabling TLS transport encryption, see Configure mongod and. Look at the field name called user and found the value as we would need this in the next step. Use TLS transport encryption to protect communications between clients and the server, including the password sent by db.createUser (). I came up with this(the script itself is small, however, comments and logging make it seem long): // script.js By default, db.createUser () sends all specified data to the MongoDB instance in cleartext, even if using passwordPrompt (). However, you can write a script to help you achieve the same result. 1 Answer Sorted by: 1 Im not sure if there is a way to do that in MongoDB as the db value when setting user roles has to be a string, source. I'm not sure if there is a way to do that in MongoDB as the db value when setting user roles has to be a string, source.
