通过 mgo 驱动程序设置 MongoDB 写关注

Set MongoDB write concern through mgo driver

我有一个 Go 客户端重复插入数据到 MongoDB 1 分钟。我不希望写入被客户端确认,直到它们被复制到我的主-辅助-仲裁副本集的辅助副本,所以我指定 majority 通过 mongo shell.

但是,客户端的执行远远早于将写入复制到辅助节点之前完成(我正在观察文档计数以确保)。如果主数据库在复制完成之前崩溃,数据将被回滚——从客户端的角度来看——已经被确认。

这种行为是否与我通过 shell 设置的多数写入问题相矛盾?我是否还需要通过 mgo 驱动程序设置写入关注?这篇文章建议我需要:

Running MongoDB Queries Concurrently With Go

We use the mgo.DialWithInfo method to create a mgo.Session object. Each session specifies a Strong or Monotonic mode, and other settings such as write concern and read preference.

但我在 mgo 驱动程序文档中没有看到任何关于设置写关注的内容。我错过了什么?

这就是您要查找的内容 (full documentation):

session.SetSafe(&mgo.Safe{WMode: "majority"})