如何构建和传递 BSON 文档 – Go 语言?

huangapple go评论97阅读模式
英文:

How to construct and pass bson document - Go lang?

问题

我正在使用Go和mongoDB来开发我的项目,使用mgo来连接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文档。

我有两个包:

  1. account.go

  2. dbEngine.go

account.go 用于创建BSON文档并将其发送给dbEngine.go。

dbEngine.go 用于建立与MongoDB的连接并插入文档。在将BSON文档传递给dbEngine.go时,使用以下代码:

dbEngine.Insert(bsonDocument);

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

func Insert(document interface{}){
    //处理逻辑
}

错误: panic: 无法将interface {}编组为BSON文档。

是否不能使用interface{}作为BSON文档?

我对Go还不熟悉。有什么建议或帮助将不胜感激。

英文:

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

 {
     "_id" : ObjectId("53439d6b89e4d7ca240668e5"),
     "balanceamount" : 3,
     "type" : "reg",
     "authentication" : {
	   "authmode" : "10",
	   "authval" : "sd",
	   "recovery" : {
		  "mobile" : "sdfsd",
		  "email" : "sds@gmail.com"
	    }
      },
     "stamps" : {
	    "in" : "x",
	    "up" : "y"
     }
  }

I have created the BSON document as above.

I have two packages

  1. account.go

  2. dbEngine.go

account.go is used to create BSON document and send the BSON document to dbEngine.go

dbEngine.go is used to establish connection to MongoDB and insert the document.
while passing BSON document to dbEngine.go

dbEngine.Insert(bsonDocument);

In dbEngine.go i am having method

func Insert(document interface{}){
 //stuff
}

> Error : panic: Can't marshal interface {} as a BSON document.

Whether interface{} is not to be used for BSON document.

I am new to Go. Any suggestion or help will be grateful

答案1

得分: 2

你不需要自己生成BSON文档。

假设在account.go文件中,你将有一个account结构体:

type Account struct {
  Id bson.ObjectId `bson:"_id"` // 导入 "labix.org/v2/mgo/bson"
  BalanceAmount int
  // 其他字段
}

然后在dbEngine.go文件中,你的Insert函数如下:

func Insert(document interface{}) {
  session, err := mgo.Dial("localhost")
  // 检查错误
  c := session.DB("db_name").C("collection_name")
  err := c.Insert(document)
}

然后,在你的应用程序的某个地方:

acc := Account{}
acc.Id = bson.NewObjectId()
acc.BalanceAmount = 3

dbEngine.Insert(&acc)
英文:

You don't need to generate a BSON document yourself.
Let say in account.go you will have an account struct:

type Account struct {
  Id bson.ObjectId `bson:"_id"` // import "labix.org/v2/mgo/bson"
  BalanceAmount int
  // Other field
}

Then in dbEngine.go your Insert function:

func Insert(document interface{}){
  session, err := mgo.Dial("localhost")
  // check error
  c := session.DB("db_name").C("collection_name")
  err := c.Insert(document)
}

And then, some where in your app:

acc := Account{}
acc.Id = bson.NewObjectId()
acc.BalanceAmount = 3

dbEngine.Insert(&acc);

答案2

得分: 2

mgo驱动程序使用labix.org/v2/mgo/bson包来处理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

英文:

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.

huangapple
  • 本文由 发表于 2014年4月8日 15:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/22930060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定