Database Manual / Reference / mongosh Methods / User Management

passwordPrompt() (mongosh method方法)

Definition定义

passwordPrompt()
Prompts for the password in mongosh. 提示输入蒙古语密码。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.输入的密码不会显示在shell中。将passwordPrompt()与接受密码作为参数的方法结合使用,而不是以明文形式向这些方法指定密码。

Examples示例

Use passwordPrompt() with db.createUser()passwordPrompt()db.createUser()一起使用

The db.createUser() requires a password to be specified.db.createUser()需要指定密码。

You can use passwordPrompt() as the value for the pwd instead of specifying the password.您可以使用passwordPrompt()作为pwd的值,而不是指定密码。

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()passwordPrompt()db.auth()一起使用

When you run the db.auth(<username>, <password>) command you can replace the password with the passwordPrompt() method.当你运行db.auth(<username>, <password>)命令时,你可以用passwordPrompt()方法替换密码。

If you omit the password from the db.auth(<username>, <password>) command, the user is prompted to enter a password.如果在db.auth(<username>, <password>)命令中省略密码,系统会提示用户输入密码。

The following example prompts the user to enter a password which is not displayed in the shell:以下示例提示用户输入未在shell中显示的密码:

db.auth("user123")

Use passwordPrompt() with db.changeUserPassword()passwordPrompt()db.changeUserPassword()一起使用

The db.changeUserPassword() requires a password to be specified.db.changeUserPassword()需要指定密码。

You can use passwordPrompt() instead of specifying the password.您可以使用passwordPrompt()代替指定密码。

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

Enter the password when prompted.出现提示时输入密码。

Use passwordPrompt() with db.updateUser()passwordPrompt()db.updateUser()一起使用

When changing the password with db.updateUser(), the method requires a password to be specified.使用db.updateUser()更改密码时,该方法需要指定密码。

You can use passwordPrompt() as the value for the pwd instead of specifying the password.您可以使用passwordPrompt()作为pwd的值,而不是指定密码。

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

Enter the password when prompted.出现提示时输入密码。