我是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]的内容时,它没有任何内容 - 全部为null。
我的映射表是这样的: m map[string]Language
而Language的定义如下:
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
cursor.Decode(e)
这一行也返回了错误。请将其结果添加到您的问题中。 - CallMeLokiDecode()
也返回错误,请打印出错误信息,如果它不是'nil'。 - CallMeLokiDecode(interface{}) error
是Decode
的定义。 - iam thadiyan