Mongodb 如何构造和传递bson文档-Go lang?

Mongodb 如何构造和传递bson文档-Go lang?,mongodb,go,Mongodb,Go,我在我的项目中使用和mongoDB,并且要连接到连接mongoDB 我有以下文件,这是要插入到MongoDB { "_id" : ObjectId("53439d6b89e4d7ca240668e5"), "balanceamount" : 3, "type" : "reg", "authentication" : { "authmode" : "10", "authval" : "sd", "recovery"

我在我的项目中使用和mongoDB,并且要连接到连接
mongoDB

我有以下文件,这是要插入到MongoDB

 {
     "_id" : ObjectId("53439d6b89e4d7ca240668e5"),
     "balanceamount" : 3,
     "type" : "reg",
     "authentication" : {
       "authmode" : "10",
       "authval" : "sd",
       "recovery" : {
          "mobile" : "sdfsd",
          "email" : "sds@gmail.com"
        }
      },
     "stamps" : {
        "in" : "x",
        "up" : "y"
     }
  }
我已如上所述创建了BSON文档

我有两个包裹

  • 去吧

  • dbEngine.go

  • account.go用于创建BSON文档并将BSON文档发送到dbEngine.go

    dbEngine.go用于建立与MongoDB的连接并插入文档。 将BSON文档传递到dbEngine.go时

    dbEngine.Insert(b文档)

    在dbEngine.go中,我有一个方法

    func Insert(document interface{}){
     //stuff
    }
    
    错误:panic:无法将接口{}封送为BSON文档

    接口{}是否不用于BSON文档


    我是新来的
    Go
    。任何建议或帮助都将不胜感激

    您无需自己生成BSON文档。
    假设在account.go中,您将拥有一个account结构:

    type Account struct {
      Id bson.ObjectId `bson:"_id"` // import "labix.org/v2/mgo/bson"
      BalanceAmount int
      // Other field
    }
    
    然后在dbEngine.go中插入函数:

    func Insert(document interface{}){
      session, err := mgo.Dial("localhost")
      // check error
      c := session.DB("db_name").C("collection_name")
      err := c.Insert(document)
    }
    
    然后,在应用程序中的某些位置:

    acc := Account{}
    acc.Id = bson.NewObjectId()
    acc.BalanceAmount = 3
    
    dbEngine.Insert(&acc);
    

    您不需要自己生成BSON文档。
    假设在account.go中,您将拥有一个account结构:

    type Account struct {
      Id bson.ObjectId `bson:"_id"` // import "labix.org/v2/mgo/bson"
      BalanceAmount int
      // Other field
    }
    
    然后在dbEngine.go中插入函数:

    func Insert(document interface{}){
      session, err := mgo.Dial("localhost")
      // check error
      c := session.DB("db_name").C("collection_name")
      err := c.Insert(document)
    }
    
    然后,在应用程序中的某些位置:

    acc := Account{}
    acc.Id = bson.NewObjectId()
    acc.BalanceAmount = 3
    
    dbEngine.Insert(&acc);
    

    mgo
    驱动程序使用包处理BSON编码/解码。在大多数情况下,这个包是按照标准库
    encoding/json
    包建模的

    因此,您可以使用结构和数组来表示对象。比如说,

    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 {
        ...
    }
    

    现在,您可以创建此类型的值以传递给
    mgo

    mgo
    驱动程序使用包处理BSON编码/解码。在大多数情况下,这个包是按照标准库
    encoding/json
    包建模的

    因此,您可以使用结构和数组来表示对象。比如说,

    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 {
        ...
    }
    
    现在可以创建此类型的值以传递给
    mgo