1 回答

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
好的,經(jīng)過(guò)一些研究,我最終找到了解決方案。有一種方法SetMode可以讓您更改特定數(shù)據(jù)庫(kù)會(huì)話(huà)的默認(rèn)一致性模式。在我們的應(yīng)用程序中,我們每次在發(fā)出請(qǐng)求之前創(chuàng)建主會(huì)話(huà)的副本,然后在完成后關(guān)閉它:
// master session is configured to use monotonic consistency
session := masterSession.Copy()
// tell mgo to read from the primary in this session
session.SetMode(mgo.Strong, true)
collection := session.DB("db").C("collection")
var obj interface{}
// now we can be sure that the following request reads the data from the primary
_ := collection.Find(bson.M{"_id": "some_id"}).One(&obj)
session.Close()
- 1 回答
- 0 關(guān)注
- 169 瀏覽
添加回答
舉報(bào)