3 回答

TA贡献1835条经验 获得超7个赞
您看到的原因是因为您使用interface{}的元素类型为Content:
Content []interface{}
如果使用interface{},它基本上不携带类型信息,驱动程序在对数组元素进行 umarshaling 时应该使用什么类型,因此驱动程序将选择/使用bson.D来表示content字段的文档。bson.D是一个切片,其中包含文档的有序字段列表,这就是为什么您会看到“数组的数组”。每个bson.D都是一个切片,代表一个文档。
type D []E
type E struct {
Key string
Value interface{}
}
如果您可以使用结构对数组元素建模,请使用它,例如:
type Foo struct {
Bar string
Baz int
}
Content []Foo `json:"content" bson:"content,omitempty"`
// Or a pointer to Foo:
Content []*Foo `json:"content" bson:"content,omitempty"`
如果您没有数组元素的固定模型,或者您可以使用bson.Mwhich is a map(但是字段/属性将是无序的,这可能是也可能不是问题):
type M map[string]interface{}
使用它:
Content []bson.M `json:"content" bson:"content,omitempty"`
- 3 回答
- 0 关注
- 137 浏览
添加回答
举报