-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename convenience functions, add Journaled, add some tests and examp…
…les.
- Loading branch information
1 parent
867a496
commit 02872bf
Showing
3 changed files
with
224 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package writeconcern_test | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"go.mongodb.org/mongo-driver/mongo/writeconcern" | ||
) | ||
|
||
// Configure a Client with write concern "majority" that requests | ||
// acknowledgement that a majority of the nodes have committed write operations. | ||
func Example_majority() { | ||
wc := writeconcern.Majority() | ||
|
||
opts := options.Client(). | ||
ApplyURI("mongodb://localhost:27017"). | ||
SetWriteConcern(wc) | ||
|
||
_, err := mongo.Connect(context.Background(), opts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Configure a Client with a write concern that requests acknowledgement that | ||
// exactly 2 nodes have committed and journaled write operations. | ||
func Example_w2Journaled() { | ||
wc := &writeconcern.WriteConcern{ | ||
W: 2, | ||
Journal: boolPtr(true), | ||
} | ||
|
||
opts := options.Client(). | ||
ApplyURI("mongodb://localhost:27017"). | ||
SetWriteConcern(wc) | ||
|
||
_, err := mongo.Connect(context.Background(), 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 | ||
} |
Oops, something went wrong.