AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

パスワードとカスタム データの変更

適切な特権を持つユーザーは、自分のパスワードやカスタム データを変更できます。Custom data によって任意のユーザー情報が保存されます。

この手順で使用する強力なパスワードを生成するには、openssl ユーティリティの rand コマンドを使用できます。例えば、次のオプションを指定して openssl rand を発行すると、48の 疑似ランダム バイトの base64 エンコードされた文字列が作成されます。

openssl rand -base64 48

自分のパスワードとカスタム データを変更するには、ユーザーのデータベースに対してそれぞれ changeOwnPassword および changeOwnCustomData アクションを付与する特権が必要です。

1

mongodロールを持つユーザーなど、ユーザーとロールを管理する特権を持つユーザーで、mongosuserAdminAnyDatabase または に接続します。次の手順では、 「 自己管理型配置でアクセス制御を有効にする 」で作成されたmyUserAdminを使用します。

mongosh --port 27017 -u myUserAdmin -p --authenticationDatabase 'admin'

If you do not specify the password to the -p command-line option, mongosh prompts for the password.

2

admin データベースで、changeOwnPassword と changeOwnCustomData を持つ新しいロールを create します。

use admin
db.createRole(
{ role: "changeOwnPasswordCustomDataRole",
privileges: [
{
resource: { db: "", collection: ""},
actions: [ "changeOwnPassword", "changeOwnCustomData" ]
}
],
roles: []
}
)
3

test データベースで、作成した "changeOwnPasswordCustomDataRole" ロールを持つ新しいロールを create します。たとえば、次の操作では、組み込みロール readWrite とユーザーが作成した"changeOwnPasswordCustomDataRole" の両方を持つユーザーが作成されます。

Tip

passwordPrompt() メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo シェルと同様に、パスワードを直接指定することもできます。

use test
db.createUser(
{
user:"user123",
pwd: passwordPrompt(), // or cleartext password
roles:[ "readWrite", { role:"changeOwnPasswordCustomDataRole", db:"admin" } ]
}
)

既存のユーザーに新しいロールを付与するには、db.grantRolesToUser() を使用します。

1

適切な権限を持つユーザーとして mongod または mongos に接続します。

たとえば、次の操作では、前提条件セクションで作成された MongoDB をuser123として接続します。

mongosh --port 27017 -u user123 --authenticationDatabase 'test' -p

If you do not specify the password to the -p command-line option, mongosh prompts for the password.

To check that you have the privileges specified in the Prerequisites section as well as to see user information, use the usersInfo command with the showPrivileges option.

2

db.updateUser() メソッドを使用して、パスワードとカスタム データをアップデートします。

たとえば、次の操作では、ユーザーのパスワードが KNlZmiaNUp0B に変更され、カスタム データが { title: "Senior Manager" } に変更されます。

Tip

passwordPrompt() メソッドを様々なユーザー認証管理メソッドやコマンドと組み合わせて使用すると、メソッドやコマンドの呼び出しでパスワードを直接指定する代わりに、パスワードの入力を求めるプロンプトが表示されます。ただし、以前のバージョンの mongo シェルと同様に、パスワードを直接指定することもできます。

use test
db.updateUser(
"user123",
{
pwd: passwordPrompt(), // or cleartext password
customData: { title: "Senior Manager" }
}
)

パスワードの入力を求められたら、入力します。