Docs Menu

Docs HomeGo

Modify Execution of CRUD Operations

On this page

  • Overview
  • Write Concern
  • Read Concern
  • Read Preference
  • Additional Information

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

You should read this guide if you need to customize the consistency and availability of the data in your replica sets.

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.

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 an Option type. The Option type has the following methods to specify the write concern:

Method
Description
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.

The following code shows how you can specify a write concern to request acknowledgement from two replica set members. The code then creates a Client with this option.

uri := "mongodb://<hostname>:<port>"
wc := writeconcern.W(2)
opts := options.Client().ApplyURI(uri).SetWriteConcern(writeconcern.New(wc))
client, err := mongo.Connect(context.TODO(), opts)

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.

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.

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("myDB")
coll := database.Collection("myCollection", opts)

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.

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.

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("myDB", opts)

For more information about the concepts in this guide, see the following Server documentation:

←  Compound OperationsAggregation →