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

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

Store slices and nested structures in MongoDB Golang

问题

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

  1. type A struct {
  2. feature []string
  3. }
  4. type B struct {
  5. title string
  6. other_feature []A
  7. }

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

英文:

I have built a program in Go with the next structure

  1. type A struct {
  2. feature []string
  3. }
  4. type B struct {
  5. title string
  6. other_feature []A
  7. }

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字段标签来指定在数据库中使用的名称。

  1. type A struct {
  2. Feature []string `bson:"feature"`
  3. }
  4. type B struct {
  5. Title string `bson:"title"`
  6. Other_feature []A `bson:"other_feature"`
  7. }
英文:

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.

  1. type A struct {
  2. Feature []string `bson:"feature"`
  3. }
  4. type B struct {
  5. Title string `bson:"title"`
  6. Other_feature []A `bson:"other_feature"`
  7. }

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:

确定