Navigation
This is an upcoming (i.e. in progress) version of the manual.

passwordPrompt()

On this page

Definition

passwordPrompt()

New in version 4.2.

Prompts for the password in the mongo shell. The entered password is not displayed in the shell. Use passwordPrompt() in conjunction with methods that accept password as a parameter instead of specifying the password in cleartext to those methods.

Examples

Use passwordPrompt() with db.createUser()

The db.createUser() requires a password to be specified.

Starting in MongoDB 4.2, you can use passwordPrompt() as the value for the pwd instead of specifying the password.

db.createUser( {
   user:"user123",
   pwd: passwordPrompt(),   // Instead of specifying the password in cleartext
   roles:[ "readWrite" ]
} )

Enter the password when prompted.

Use passwordPrompt() with db.auth()

The db.auth() requires a password to be specified.

Starting in MongoDB 4.2, you can use passwordPrompt() as the value for the pwd instead of specifying the password.

db.auth("user123", passwordPrompt())

db.auth( {
   user: "user123",
   pwd: passwordPrompt()  // Instead of specifying the password in cleartext
} )

Enter the password when prompted.

Use passwordPrompt() with db.changeUserPassword()

The db.changeUserPassword() requires a password to be specified.

Starting in MongoDB 4.2, you can use passwordPrompt() instead of specifying the password.

db.changeUserPassword("user123", passwordPrompt())

Enter the password when prompted.

Use passwordPrompt() with db.updateUser()

When changing the password with db.updateUser(), the method requires a password to be specified.

Starting in MongoDB 4.2, you can use passwordPrompt() as the value for the pwd instead of specifying the password.

db.updateUser(
   "user123",
   {
     pwd: passwordPrompt(),
     mechanisms: [ "SCRAM-SHA-256" ]
   }
)

Enter the password when prompted.