英文:
Set MongoDB write concern through mgo driver
问题
我有一个 Go 客户端,它会在 1 分钟内反复向 MongoDB 插入数据。我希望在数据被复制到主从副本集的从节点之前,不向客户端确认写入操作,因此我通过 mongo shell 指定了 majority 写关注。
然而,客户端的执行完成时间远远早于数据被复制到从节点(我通过文档计数来确认)。如果主节点在复制完成之前崩溃,那么从客户端的角度来看,已经被确认的数据将会被回滚。
这个行为是否与我通过 shell 设置的 majority 写关注相矛盾?我是否还需要通过 mgo 驱动设置写关注?这篇文章建议我需要:
> 我们使用 mgo.DialWithInfo
方法创建 mgo.Session
对象。每个会话都指定了 Strong 或 Monotonic 模式,以及其他设置,如写关注和读偏好。
但是我在 mgo
驱动的文档中没有看到设置写关注的内容。我漏掉了什么吗?
英文:
I have a Go client that repeatedly inserts data into MongoDB for 1 minute. I don't want the writes to be acknowledged to the client until they are replicated to the secondary of my primary-secondary-arbiter replica set, so I specified majority write concern through the mongo shell.
However, the client's execution completes far ahead of the writes being replicated to the secondary (I'm watching the doc count to be sure). If the primary were to crash before the replication is done, data would be rolled back that--from the client's perspective--had already been acknowledged.
Doesn't this behavior contradict the majority write concern I set through the shell? Do I also need to set the write concern through the mgo driver? This article suggests I need to:
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.
But I don't see anything in the mgo
driver docs about setting write concern. What am I missing?
答案1
得分: 1
这是你要找的内容(完整文档):
session.SetSafe(&mgo.Safe{WMode: "majority"})
英文:
This is what you are looking for (full documentation):
session.SetSafe(&mgo.Safe{WMode: "majority"})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论