How to construct and pass bson document - Go lang?

后端 未结 2 1245
面向向阳花
面向向阳花 2020-12-21 18:21

I am using Go and mongoDB in my project and mgo is to connect to connect MongoDB.

I am having following document this is to be inserted in the MongoDB

2条回答
  •  难免孤独
    2020-12-21 19:08

    The mgo driver uses the labix.org/v2/mgo/bson package to handle BSON encoding/decoding. For the most part, this package is modelled after the standard library encoding/json package.

    So you can use structs and arrays to represent objects. For example,

    type Document struct {
        Id bson.ObjectId `bson:"_id"`
        BalanceAmount int `bson:"balanceamount"`
        Type string `bson:"type"`
        Authentication Authentication `bson:"authentication"`
        Stamps Stamps `bson:"stamps"`
    }
    type Authentication struct {
        ...
    }
    type Stamps struct {
        ...
    }
    

    You can now create values of this type to pass to mgo.

提交回复
热议问题