【发布时间】:2019-01-05 17:24:47
【问题描述】:
我是 mongodb-go-driver 的新手。但我被困住了。
cursor, e := collection.Find(context.Background(), bson.NewDocument(bson.EC.String("name", id)))
for cursor.Next(context.Background()) {
e := bson.NewDocument()
cursor.Decode(e)
b, _ := e.MarshalBSON()
err := bson.Unmarshal(b, m[id])
}
当查看 m[id] 的内容时,它没有内容 - 全部为空。
我的地图是这样的: m map[string]语言
语言定义如下:
type Language struct {
ID string `json:"id" bson:"_id"` // is this wrong?
Name string `json:"name" bson:"name"`
Vowels []string `json:"vowels" bson:"vowels"`
Consonants []string `json:"consonants" bson:"consonants"`
}
我做错了什么?
我正在学习使用这个例子:https://github.com/mongodb/mongo-go-driver/blob/master/examples/documentation_examples/examples.go
【问题讨论】:
-
line
cursor.Decode(e)也返回错误。将其结果添加到您的问题中 -
没有。如果我打印 e 我会看到来自 MongoDB 的有效 BSON 数据
-
我的意思是函数
Decode()也返回错误。如果不是 nil,则在此处打印错误 -
是的,你是对的。
Decode(interface{}) error是Decode的定义