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

cursor.readPref()(mongoshメソッド)

cursor.readPref(mode, tagSet, hedgeOptions)

重要

mongosh メソッド

This page documents a mongosh method. 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

次の 読み取り設定 モードのいずれか: primary、primaryPreferred、secondary、secondaryPreferred、または nearest

ドキュメントの配列

任意。 指定されたタグを持つメンバーへの読み取りをターゲットにするために使用されるタグセット。 tagSetは使用できませんprimary

詳しくは、「読み込み設定(read preference)のタグセット リスト」を参照してください。

ドキュメント

任意。 ヘッジの使用を有効にするかどうかを指定するドキュメント。

{ enabled: <boolean> }

enabled フィールドのデフォルトは true です。つまり、空のドキュメント { } を指定することは { enabled: true } を指定することと同じです。

ヘッジされた読み取り は シャーディングされたクラスター で利用できます。 ヘッジされた読み取りを使用するには、 mongosにヘッジされた読み取り用のenabled support (デフォルト)と、 primary以外の 読み込み設定( read preference ) がヘッジされた読み取りの使用を有効にする必要があります。

読み込み設定(read preference)nearest により、シャーディングされたクラスターでのヘッジ読み取りの使用がデフォルトで有効になります。つまり、デフォルトでは { enabled: true } になります。

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)モードを使用して、セカンダリ メンバーへの読み取りをターゲットにします。

db.collection.find({ }).readPref( "secondary")

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
    )
このページを評価

項目一覧