In mongodb-go-driver, how to marshal/unmarshal BSON in to a struct

后端 未结 4 860
野趣味
野趣味 2021-02-05 21:19

I am new to mongodb-go-driver. But i am stuck.

cursor, e := collection.Find(context.Background(), bson.NewDocument(bson.EC.String(\"name\", id)))

for cursor.Nex         


        
4条回答
  •  旧时难觅i
    2021-02-05 22:14

    Update: New changes were made recently, now have to change to:

    import "github.com/mongodb/mongo-go-driver/bson/primitive"
    
    type Language struct {
        ID         primitive.ObjectID `bson:"_id,omitempty"` // omitempty to protect against zeroed _id insertion         
        Name       string   `json:"name" bson:"name"`
        Vowels     []string `json:"vowels" bson:"vowels"`
        Consonants []string `json:"consonants" bson:"consonants"`
    }
    

    And take the ID value:

    log.Println("lang id:", objLang.ID)
    ...
    

提交回复
热议问题