Documentation ¶
Overview ¶
Package writeconcern defines write concerns for MongoDB operations.
For more information about MongoDB write concerns, see https://www.mongodb.com/docs/manual/reference/write-concern/
Example (Majority) ¶
Configure a Client with write concern "majority" that requests acknowledgement that a majority of the nodes have committed write operations.
package main import ( "go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" ) func main() { wc := writeconcern.Majority() opts := options.Client(). ApplyURI("mongodb://localhost:27017"). SetWriteConcern(wc) _, err := mongo.Connect(opts) if err != nil { panic(err) } }
Output:
Example (W2Journaled) ¶
Configure a Client with a write concern that requests acknowledgement that exactly 2 nodes have committed and journaled write operations.
package main import ( "go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" ) func main() { wc := &writeconcern.WriteConcern{ W: 2, Journal: boolPtr(true), } opts := options.Client(). ApplyURI("mongodb://localhost:27017"). SetWriteConcern(wc) _, err := mongo.Connect(opts) if err != nil { panic(err) } } // boolPtr is a helper function to convert a bool constant into a bool pointer. // // If you're using a version of Go that supports generics, you can define a // generic version of this function that works with any type. For example: // // func ptr[T any](v T) *T { // return &v // } func boolPtr(b bool) *bool { return &b }
Output:
Index ¶
Examples ¶
Constants ¶
const WCMajority = "majority"
WCMajority can be used to create a WriteConcern with a W value of "majority".
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type WriteConcern ¶
type WriteConcern struct { // W requests acknowledgment that the write operation has propagated to a // specified number of mongod instances or to mongod instances with // specified tags. It sets the "w" option in a MongoDB write concern. // // W values must be a string or an int. // // Common values are: // - "majority": requests acknowledgment that write operations have been // durably committed to the calculated majority of the data-bearing // voting members. // - 1: requests acknowledgment that write operations have been written // to 1 node. // - 0: requests no acknowledgment of write operations // // For more information about the "w" option, see // https://www.mongodb.com/docs/manual/reference/write-concern/#w-option W interface{} // Journal requests acknowledgment from MongoDB that the write operation has // been written to the on-disk journal. It sets the "j" option in a MongoDB // write concern. // // For more information about the "j" option, see // https://www.mongodb.com/docs/manual/reference/write-concern/#j-option Journal *bool }
A WriteConcern defines a MongoDB write concern, which describes the level of acknowledgment requested from MongoDB for write operations to a standalone mongod, to replica sets, or to sharded clusters.
For more information about MongoDB write concerns, see https://www.mongodb.com/docs/manual/reference/write-concern/
func Custom ¶
func Custom(tag string) *WriteConcern
Custom returns a WriteConcern that requests acknowledgment that write operations have propagated to tagged members that satisfy the custom write concern defined in "settings.getLastErrorModes".
For more information about custom write concern names, see https://www.mongodb.com/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-custom-write-concern-name-
func Journaled ¶
func Journaled() *WriteConcern
Journaled returns a WriteConcern that requests acknowledgment that write operations have been written to the on-disk journal on MongoDB.
The database's default value for "w" determines how many nodes must write to their on-disk journal before the write operation is acknowledged.
For more information about write concern "j: true", see https://www.mongodb.com/docs/manual/reference/write-concern/#mongodb-writeconcern-ournal
func Majority ¶
func Majority() *WriteConcern
Majority returns a WriteConcern that requests acknowledgment that write operations have been durably committed to the calculated majority of the data-bearing voting members.
Write concern "w: majority" typically requires write operations to be written to the on-disk journal before they are acknowledged, unless journaling is disabled on MongoDB or the "writeConcernMajorityJournalDefault" replica set configuration is set to false.
For more information about write concern "w: majority", see https://www.mongodb.com/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-
func Unacknowledged ¶
func Unacknowledged() *WriteConcern
Unacknowledged returns a WriteConcern that requests no acknowledgment of write operations.
For more information about write concern "w: 0", see https://www.mongodb.com/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-number-
func W1 ¶
func W1() *WriteConcern
W1 returns a WriteConcern that requests acknowledgment that write operations have been written to memory on one node (e.g. the standalone mongod or the primary in a replica set).
For more information about write concern "w: 1", see https://www.mongodb.com/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-number-
func (*WriteConcern) Acknowledged ¶
func (wc *WriteConcern) Acknowledged() bool
Acknowledged indicates whether or not a write with the given write concern will be acknowledged.
func (*WriteConcern) IsValid ¶
func (wc *WriteConcern) IsValid() bool
IsValid returns true if the WriteConcern is valid.