CRUD 操作の実行を変更
Overview
このガイドでは、MongoDB Go ドライバーがレプリカセットの書込み保証、読み取り保証、 読み込み設定( read preference ) 構成を使用して、作成、読み取り、更新、削除(CRUD)操作を実行する方法を変更する方法を学習します。
次のレベルで、書込み保証、読み取り保証、読み込み設定(read preference)のオプションを設定できます。
クライアント レベル。オーバーライドされない限り、すべての操作実行にデフォルトを設定します
セッション レベル
トランザクション レベル
データベース レベル
コレクション レベル
Query level
レプリカセット内のデータの整合性と可用性をカスタマイズする必要がある場合は、このガイドをお読みください。
書込み保証 (write concern)
書込み保証 (write concern) は、挿入やアップデートなどの書込み (write) 操作が成功として返される前に、この操作を確認する必要があるレプリカセット内のデータを保持するノードの数を示します。 デフォルトでは、 書込み保証 (write concern) では、書込み (write) 操作が成功と見なされる前に、 プライマリ レプリカセット のノードのみが書込み (write) 操作を確認する必要があります。
オプション
MongoDB Goドライバーには、レプリカセットの 書込み保証 ( 書込み保証 (write concern) ) を指定できるwriteconcern
パッケージが用意されています。 Option
タイプのSetWriteConcern()
メソッドを使用して書込み保証( 書込み保証 (write concern) )を設定します。 Option
タイプでは、書込み保証( 書込み保証 (write concern))を指定するための次のメソッドがあります。
方式 | 説明 |
---|---|
J() | The client requests acknowledgement that write operations are written to the
journal. For more information, see the
Write Concern specification. Parameter: bool |
W() | The client requests acknowledgement that write operations propagate to the
specified number of mongod instances. For more information, see the
Write Concern specification.Parameter: int |
WMajority() | The client requests acknowledgement that write operations propagate to the
majority of mongod instances. For more information, see the
Write Concern specification.Parameter: none |
WTagSet() | The client requests acknowledgement that write operations propagate to the
specified mongod instance. For more
information, see the Write Concern specification.Parameter: string |
Tip
You can alternatively specify a write concern in your connection string. See the Server Manual entry on Write Concern Options for more information.
例
次のコードは、書込み保証 (write concern) を指定して、2 つのレプリカセット ノードに確認応答を要求する方法を示しています。 次に、コードはこのオプションを持つClient
を作成します。
uri := "mongodb://<hostname>:<port>" wc := writeconcern.W(2) opts := options.Client().ApplyURI(uri).SetWriteConcern(writeconcern.New(wc)) client, err := mongo.Connect(context.TODO(), opts)
読み取り保証(read concern)
読み取り保証(read concern)オプションを使用すると、クライアントがクエリから返すデータを決定できます。 デフォルトの読み取り保証 (read concern) レベルは "local" です。つまり、クライアントはインスタンスの最新データを返しますが、そのデータがレプリカセットのノードの過半数に書き込まれたことは保証されません。
オプション
MongoDB Go ドライバーにはreadconcern
パッケージが用意されており、レプリカセットの読み取り保証(read concern)を指定できます。 ReadConcern
タイプのSetReadConcern()
メソッドを使用して読み取り保証を設定します。 ReadConcern
タイプでは、読み取り保証を指定するための次のメソッドがあります。
方式 | 説明 |
---|---|
Available() | The query returns data from the instance
with no guarantee that the data has been written to a majority of
the replica set members. For more information, see the
Read Concern specification. |
Linearizable() | The query returns data that reflects all
successful writes issued with a write concern of majority and
acknowledged prior to the start of the read operation. For more information, see the
Read Concern specification. |
Local() | The query returns the instance’s most recent
data. For more information, see the
Read Concern specification. |
Majority() | The query returns the instance’s most recent
data acknowledged as having been written to a majority of members
in the replica set. For more information, see the
Read Concern specification. |
Snapshot() | The query returns a complete copy of the
data in a mongod instance at a specific point in time. Only
available for operations within multi-document transactions. For more information, see the
Read Concern specification. |
例
次のコードは、「majority」の読み取り保証(read concern)を指定する方法を示しています。 次に、コードはこのオプションを持つCollection
を選択します。
rc := readconcern.Majority() opts := options.Collection().SetReadConcern(rc) database := client.Database("myDB") coll := database.Collection("myCollection", opts)
読み込み設定 (read preference)
読み込み設定(read preference)オプションは、MongoDB クライアントが読み取り操作を レプリカセット のノードにルーティングする方法を指定します。 デフォルトでは、アプリケーションの読み取り操作は、 レプリカセット 内の プライマリ を対象に行われます。
Read preference consists of the read preference mode and, optionally, a tag set list, the maxStalenessSeconds option, and the hedged read option.
オプション
MongoDB Go ドライバーには、レプリカセットの読み込み設定(read preference)を指定できるreadpref
パッケージが用意されています。 読み込み設定(read preference)を設定するには、 SetReadPreference()
メソッドをReadPref
タイプで使用します。 ReadPref
型には、読み込み設定(read preference)を指定するための次のメソッドがあります。
方式 | 説明 |
---|---|
Nearest() | The client reads from a random eligible replica set member,
primary or secondary, based on a specified latency threshold. For more information, see the
Read Preference Server Manual entry. |
Primary() | The client reads from the current replica set primary node. For more information, see the
Read Preference Server Manual entry. |
PrimaryPreferred() | The client reads from the primary node in most situations. If the primary is
unavailable, operations read from secondary members. For more
information, see the Read Preference Server Manual entry. |
Secondary() | The client reads from the secondary members of the replica set. For more information, see the
Read Preference Server Manual entry. |
SecondaryPreferred() | The client reads from the secondary nodes in most situations. If the secondaries are
unavailable, operations read from the primary member. For more information, see the
Read Preference Server Manual entry. |
Tip
You can alternatively specify a read preference in your connection string. See the Server Manual entry on Read Preference Options for more information.
例
次のコードは、セカンダリ ノードから読み取るための読み込み設定 (read preference) を指定する方法を示しています。 次に、コードはこのオプションを持つDatabase
を選択します。
rp := readpref.Secondary() opts := options.Database().SetReadPreference(rp) database := client.Database("myDB", opts)
詳細情報
このガイドの概念の詳細については、次のサーバー ドキュメントを参照してください。