目前我們正在將mgo ( globalsign ) 驅動程式遷移到mongo-driver
我想要一些替代方法Find.().One()
我嘗試了類似下面的方法,但沒有幫助
login = model.LoginModel{}
err = mongo.Collection.Find(bson.M{"name": MAXCOUNT}).Decode(&loginCount)
以以下錯誤回傳我,
error was: cannot transform type []interface {} to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel
不確定新的解碼方法是否允許結構值?
我的結構如下所示
type LoginModel struct {
Username string `json:"username"`
Password string `json:"password"`
}
我也需要有相應的 bson 值嗎?
試圖在 go-mongo-driver 中運行 Find.().One()
uj5u.com熱心網友回復:
Collection.Find()
旨在查詢多個元素。它回傳 a mongo.Cursor
,您可以使用它來迭代結果或使用Cursor.All()
.
如果您需要單個結果,請Collection.FindOne()
改用。
例如:
ctx := context.Background() // Use / setup your context
c := ... // acquire mongo.Collection
var login model.LoginModel
err = c.FindOne(ctx, bson.M{"name": MAXCOUNT}).Decode(&login)
// check error
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/522644.html