在MongoDB Golang中存储切片和嵌套结构。

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

Store slices and nested structures in MongoDB Golang

问题

我已经用Go语言构建了一个具有以下结构的程序:

type A struct {
  feature []string
}

type B struct {
  title         string
  other_feature []A
}

我尝试使用bson包,但在执行后,只有title出现在数据库中。有人有解决方案吗?

英文:

I have built a program in Go with the next structure

type A struct {
  feature []string
}

type B struct {
  title string
  other_feature []A
}

I tried to use the bson package but only the title appears on the database after execution. Does anyone have a solution?

答案1

得分: 5

你需要通过将字段名的首字母大写来导出字段名。使用bson字段标签来指定在数据库中使用的名称。

type A struct {
  Feature []string `bson:"feature"`
}

type B struct {
  Title         string       `bson:"title"`
  Other_feature []A          `bson:"other_feature"`
}
英文:

You need to export the field names by starting the field name with an uppercase letter. Use the bson field tag to specify the named used in the database.

type A struct {
  Feature []string `bson:"feature"`
}

type B struct {
  Title string       `bson:"title"`
  Other_feature []A  `bson:"other_feature"`
}

huangapple
  • 本文由 发表于 2015年11月20日 05:07:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/33814370.html
匿名

发表评论

匿名网友

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

确定