通过mgo驱动设置MongoDB的写关注度

huangapple go评论82阅读模式
英文:

Set MongoDB write concern through mgo driver

问题

我有一个 Go 客户端,它会在 1 分钟内反复向 MongoDB 插入数据。我希望在数据被复制到主从副本集的从节点之前,不向客户端确认写入操作,因此我通过 mongo shell 指定了 majority 写关注。

然而,客户端的执行完成时间远远早于数据被复制到从节点(我通过文档计数来确认)。如果主节点在复制完成之前崩溃,那么从客户端的角度来看,已经被确认的数据将会被回滚。

这个行为是否与我通过 shell 设置的 majority 写关注相矛盾?我是否还需要通过 mgo 驱动设置写关注?这篇文章建议我需要:

使用 Go 并发运行 MongoDB 查询

> 我们使用 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"})

huangapple
  • 本文由 发表于 2015年7月30日 00:21:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/31706017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定