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 關注
- 187 瀏覽
添加回答
舉報
