There is a mongodb cluster (1 master 2 replicas)
Updating records in a larger number and for this used BulkWrite, need to call next BulkWrite after replicas caught up master, need to make sure that the replicas have already caught up with the master for this request. Used go.mongodb.org/mongo-driver/mongo
Write propagation can be "controlled" with a writeconcern.WriteConcern
.
WriteConcern
can be created using different writeconcern.Option
s. The writeconcern.W()
function can be used to create a writeconcern.Option
that controls how many instances writes must be progatated to (or more specifically the write operation will wait for acknowledgements from the given number of instances):
func W(w int) Option
W requests acknowledgement that write operations propagate to the specified number of mongod instances.
So first you need to create a writeconcern.WriteConcern
that is properly configured to wait for acknowledgements from 2 nodes in your case (you have 1 master and 2 replicas):
wc := writeconcern.New(writeconcern.W(2))
You now have to choose the scope of this write concern: it may be applied on the entire mongo.Client
, it may be applied on a mongo.Database
or just applied on a mongo.Collection
.
Creating a client, obtaining a database or just a collection all have options which allow specifying the write concern, so this is really up to you where you want it to be applied.
If you just want this write concern to have effect on certain updates, it's enough to apply it on mongo.Collection
which you use to perform the updates:
var client *mongo.Client // Initialize / connect client
wc := writeconcern.New(writeconcern.W(2))
c := client.Database("<your db name>").
Collection("<your collection name>",
options.Collection().SetWriteConcern(wc))
Now if you use this c
collection to perform the writes (updates), the wc
write concern will be used / applied, that is, each write operation will wait for acknowledgements of the writes propagated to 2 instances.
If you would apply the wc
write concert on the mongo.Client
, then that would be the default write concern for everything you do with that client, if you'd apply it on a mongo.Database
, then it would be the default for everything you do with that database. Of course the default can be overridden, just how we applied wc
on the c
collection in the above example.