passwordPrompt()
On this page本页内容
Definition定义
passwordPrompt()New in version 4.2.4.2版新增。Prompts for the password in在mongosh.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.passwordPrompt()与接受密码作为参数的方法结合使用,而不是在这些方法的明文中指定密码。
Examples实例
Use passwordPrompt() with db.createUser()将passwordPrompt()与db.createUser()一起使用
passwordPrompt() with db.createUser()The db.createUser() requires a password to be specified.db.createUser()需要指定密码。
Starting in MongoDB 4.2, you can use 从MongoDB 4.2开始,您可以使用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()一起使用
passwordPrompt() with db.auth()Starting in MongoDB 4.2, when you run the 从MongoDB 4.2开始,当您运行db.auth(<username>, <password>) command you can replace the password with the passwordPrompt() method.db.auth(<username>, <password>)命令时,您可以用passwordPrompt()方法替换密码。
Starting in MongoDB 4.4, if you omit the password from the 从MongoDB 4.4开始,如果从db.auth(<username>, <password>) command, the user is prompted to enter a password.db.auth(<username>, <password>)命令中省略密码,则会提示用户输入密码。
Both of the following examples prompt the user to enter a password which is not displayed in the shell:以下两个示例都会提示用户输入shell中未显示的密码:
// Starting in MongoDB 4.2
db.auth("user123", passwordPrompt())
// Starting in MongoDB 4.4
db.auth("user123")
Use passwordPrompt() with db.changeUserPassword()将passwordPrompt()与db.changeUserPassword()一起使用
passwordPrompt() with db.changeUserPassword()The db.changeUserPassword() requires a password to be specified.db.changeUserPassword()需要指定密码。
Starting in MongoDB 4.2, you can use 从MongoDB 4.2开始,您可以使用passwordPrompt() instead of specifying the password.passwordPrompt()代替指定密码。
db.changeUserPassword("user123", passwordPrompt())
Enter the password when prompted.提示时输入密码。
Use passwordPrompt() with db.updateUser()将passwordPrompt()与db.updateUser()一起使用
passwordPrompt() with db.updateUser()When changing the password with 使用db.updateUser(), the method requires a password to be specified.db.updateUser()更改密码时,该方法需要指定密码。
Starting in MongoDB 4.2, you can use 从MongoDB 4.2开始,您可以使用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.提示时输入密码。