英文:
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"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论