定義
cursor.readPref(mode, tagSet, hedgeOptions)重要
mongosh メソッド
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。
Append
readPref()to a cursor to control how the client routes the query to members of the replica set.注意
You must apply
readPref()to the cursor before retrieving any documents from the database.
パラメーター
Parameter | タイプ | 説明 |
|---|---|---|
string | 次の 読み取り設定 モードのいずれか: | |
ドキュメントの配列 | 任意。 指定されたタグを持つメンバーへの読み取りをターゲットにするために使用されるタグセット。 詳しくは、「読み込み設定(read preference)のタグセット リスト」を参照してください。 | |
ドキュメント | 任意。 ヘッジの使用を有効にするかどうかを指定するドキュメント。
ヘッジされた読み取り は シャーディングされたクラスター で利用できます。 ヘッジされた読み取りを使用するには、 読み込み設定(read preference) |
readPref() does not support the Read Preference maxStalenessSeconds option for read preference.
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
例
読み込み設定(read preference)モードを指定する
次の操作では、 読み込み設定(read preference)モードを使用して、セカンダリ メンバーへの読み取りをターゲットにします。
db.collection.find({ }).readPref( "secondary")
読み込み設定(read preference)のタグセットを指定する
To target secondaries with specific tags, include both the mode and the tagSet array:
db.collection.find({ }).readPref( "secondary", [ { "datacenter": "B" }, // First, try matching by the datacenter tag { "region": "West"}, // If not found, then try matching by the region tag { } // If not found, then use the empty document to match all eligible members ] )
セカンダリ選択プロセス中に、MongoDB は最初に datacenter: "B" タグを持つセカンダリ ノードを検索しようとします。
見つかった場合、MongoDB は適格なセカンダリを
datacenter: "B"タグを持つものに制限し、残りのタグを無視します。何も見つからない場合、MongoDB は
"region": "West"タグを持つセカンダリ ノードを検索しようとします。見つかった場合、MongoDB は適格なセカンダリを
"region": "West"タグのあるものに制限します。何も見つからない場合、MongoDB は適格なセカンダリを使用します。
詳しくは、「タグの一致順序」を参照してください。
ヘッジされた読み取りを指定する
シャーディングされたクラスターの場合、非プライマリ 読み込み設定( read preference ) で ヘッジされ た読み取りを有効にできます。ヘッジされた読み取りを使用するには、 mongosにヘッジされた読み取り用のenabled support (デフォルト)と、 primary以外の 読み込み設定( read preference ) でヘッジされた読み取りの使用が有効になっている必要があります。
To target secondaries on sharded clusters using hedged reads, include both the mode and the hedgeOptions, as in the following examples:
タグセットなし
db.collection.find({ }).readPref( "secondary", // mode null, // tag set { enabled: true } // hedge options ) タグセットあり
db.collection.find({ }).readPref( "secondary", // mode [ { "datacenter": "B" }, { } ], // tag set { enabled: true } // hedge options )