Modify Execution of CRUD Operations
Overview
In this guide, you can learn how to modify the way that the MongoDB Go Driver executes create, read, update, and delete (CRUD) operations using write concern, read concern, and read preference configurations for replica sets.
You can set write concern, read concern, and read preference options at the following levels:
Client level, which sets the default for all operation executions unless overridden
Session level
Transaction level
Database level
Collection level
In the following sections, you can learn how to customize the consistency and availability of the data in your replica sets.
Write Concern
A write concern describes the number of data-bearing members in a replica set that must acknowledge a write operation, such as an insert or update, before the operation is returned as successful. By default, the write concern requires only the primary replica set member to acknowledge the write operation before the operation is deemed successful.
Options
The MongoDB Go Driver provides the writeconcern
package, which lets
you specify the write concern for a replica set. Set the write
concern using the SetWriteConcern()
method with a WriteConcern
type. The WriteConcern
type has the following methods to
select common write concern specifications:
Method | Description |
---|---|
Custom() | The client requests acknowledgement that write operations propagate to
tagged members of a mongod instance. For more
information, see the Write Concern specification.Parameter: string |
Journaled() | The client requests acknowledgement that write operations are
written to the on-disk journal. For more information, see the
Write Concern specification. Parameter: none |
Majority() | The client requests acknowledgement that write operations propagate to the
majority of data-bearing voting members. For more information, see the
Write Concern specification. Parameter: none |
Unacknowledged() | The client requests requests no acknowledgment of write
operations. For more information, see the
Write Concern specification for w: 0. Parameter: none |
W1() | The client requests acknowledgement that write operations have
been written to memory on one node, such as the standalone mongod or
the primary in a replica set. For more
information, see the Write Concern specification for w: 1. Parameter: none |
Tip
Write Concern Timeout
You cannot set a timeout on a WriteConcern
instance. Instead, set
the timeout at the operation level by using the WithTimeout()
method when creating a Context. To learn more, see
Single Timeout Setting in the Connection Options guide.
If you require a more specialized write concern, you can define a custom
WriteConcern
struct literal. You can set the following fields in a
WriteConcern
struct:
Field | Description |
---|---|
W | Specifies the number of mongod instances or tagged members
that write operations must propagate to for acknowledgement. Common values include
1 , 0 , and "majority" .Type: string or int |
Journal | Specifies if write operations must be written to the on-disk
journal for acknowledgement. Type: bool |
Tip
You can alternatively specify a write concern in your connection string. See the Server manual entry on Write Concern Options for more information.
Example
The following code shows how you can specify different write concerns
at the client and collection level. The client-level write concern
requests acknowledgement from two replica set members and sets journaling to
false
. The collection-level write concern requests
acknowledgement from the majority of replica set members.
uri := "mongodb://<hostname>:<port>" journal := false cliWC := &writeconcern.WriteConcern{ W: 2, Journal: &journal, } clOpts := options.Client().ApplyURI(uri).SetWriteConcern(cliWC) client, err := mongo.Connect(clOpts) ... collWC := writeconcern.Majority() collOpts := options.Collection().SetWriteConcern(collWC) coll := client.Database("db").Collection("myColl", collOpts)
Read Concern
The read concern option allows you to determine which data the client returns from a query. The default read concern level is "local", meaning that the client returns the instance’s most recent data, with no guarantee that the data has been written to a majority of the replica set members.
Options
The MongoDB Go Driver provides the readconcern
package, which lets
you specify the read concern for a replica set. Set the read concern using the
SetReadConcern()
method with a ReadConcern
type. The ReadConcern
type has the following methods to specify the read concern:
Method | Description |
---|---|
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. |
Example
The following code shows how you can specify a read concern of
"majority". The code then selects a Collection
with this option.
rc := readconcern.Majority() opts := options.Collection().SetReadConcern(rc) database := client.Database("db") coll := database.Collection("myCollection", opts)
Read Preference
The read preference option specifies how the MongoDB client routes read operations to the members of a replica set. By default, an application directs its read operations to the primary member in a replica set.
Read preference consists of the read preference mode and, optionally, a tag set list, the maxStalenessSeconds option, and the hedged read option.
Options
The MongoDB Go Driver provides the readpref
package, which lets
you specify the read preference for a replica set. Set the read preference using the
SetReadPreference()
method with a ReadPref
type. The ReadPref
type has the following methods to specify the read preference:
Method | Description |
---|---|
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.
Example
The following code shows how you can specify a read preference to read
from secondary nodes. The code then selects a Database
with this option.
rp := readpref.Secondary() opts := options.Database().SetReadPreference(rp) database := client.Database("db", opts)
Additional Information
For more information about the concepts in this guide, see the following Server documentation: