passwordPrompt()

Definition

  • passwordPrompt()

New in version 4.2.

Prompts for the password in the mongo shell. Theentered password is not displayed in the shell. UsepasswordPrompt() in conjunction with methods that acceptpassword as a parameter instead of specifying the password incleartext 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 thevalue for the pwd instead of specifying the password.

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

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 thevalue for the pwd instead of specifying the password.

  1. db.auth("user123", passwordPrompt())
  2.  
  3. db.auth( {
  4. user: "user123",
  5. pwd: passwordPrompt() // Instead of specifying the password in cleartext
  6. } )

Enter the password when prompted.

Use passwordPrompt() with db.changeUserPassword()

The db.changeUserPassword() requires a password to bespecified.

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

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

Enter the password when prompted.

Use passwordPrompt() with db.updateUser()

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

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

  1. db.updateUser(
  2. "user123",
  3. {
  4. pwd: passwordPrompt(),
  5. mechanisms: [ "SCRAM-SHA-256" ]
  6. }
  7. )

Enter the password when prompted.