猿问

在mongodb-go-driver中,如何将BSON编组/解组为结构

我是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


语言定义如下:


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


千巷猫影
浏览 378回答 3
3回答
随时随地看视频慕课网APP

相关分类

Go
我要回答