MongoDB Go Driver 无法正确解组嵌套文档
MongoDB Go Driver unable to unmarshal nested documents correctly
我有一个 setter 和 getter 对集合中的特定字段进行操作。 setter 工作正常并且文档按预期更新,但是 getter 没有 return 正确填充结构。 我做错了什么?
作为 Go 结构的集合:
type Model struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
EntityType string `bson:"entity_type,omitempty"`
EntityID string `bson:"entity_id,omitempty"`
ConfigSource ConfigSources `bson:"config_source,inline,omitempty"`
}
type ConfigSources struct {
Configs []ConfigSource `bson:"configs,omitempty"`
}
type ConfigSource struct {
Hour int `bson:"hour"`
Source string `bson:"source"`
}
Setter 片段:
cfg := ConfigSources{
Configs: []ConfigSource{
{
Hour: 1,
Source: "Hour_1_Source",
},
{
Hour: 2,
Source: "Hour_2_Source",
},
},
}
c := db.Collection("foo")
selectorQuery := bson.M{"entity_id": entityId}
updateQuery := bson.M{"$set": bson.M{"config_source": configName}}
result, err := c.UpdateMany(ctx, selectorQuery, updateQuery)
Getter 片段:
c := db.Collection("foo")
q, err := c.Find(ctx, bson.M{"_id": bson.M{"$in": idsImQuerying}})
if err != nil {
return nil
}
var results []Model
err = q.All(ctx, &results)
fmt.Printf("\n\n\n\n%#v\n\n\n\n", results) // this output is shown below
得到的结果:
[]Model{
Model{
ID:primitive.ObjectID{0x5a, 0xa9, 0x7a, 0x40, 0xdf, 0xe5, 0x90, 0x44, 0x49, 0xdb, 0x61, 0x4},
EntityType:"CELL",
EntityID:"4110902605985611776",
ConfigSource:ConfigSources{
Configs:[]ConfigSource(nil)
}
}
}
在图集中查看的字段:
从模型结构中删除内联后,它工作正常
type Model struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
EntityType string `bson:"entity_type,omitempty"`
EntityID string `bson:"entity_id,omitempty"`
ConfigSource ConfigSources `bson:"config_source,omitempty"`
}
我有一个 setter 和 getter 对集合中的特定字段进行操作。 setter 工作正常并且文档按预期更新,但是 getter 没有 return 正确填充结构。 我做错了什么?
作为 Go 结构的集合:
type Model struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
EntityType string `bson:"entity_type,omitempty"`
EntityID string `bson:"entity_id,omitempty"`
ConfigSource ConfigSources `bson:"config_source,inline,omitempty"`
}
type ConfigSources struct {
Configs []ConfigSource `bson:"configs,omitempty"`
}
type ConfigSource struct {
Hour int `bson:"hour"`
Source string `bson:"source"`
}
Setter 片段:
cfg := ConfigSources{
Configs: []ConfigSource{
{
Hour: 1,
Source: "Hour_1_Source",
},
{
Hour: 2,
Source: "Hour_2_Source",
},
},
}
c := db.Collection("foo")
selectorQuery := bson.M{"entity_id": entityId}
updateQuery := bson.M{"$set": bson.M{"config_source": configName}}
result, err := c.UpdateMany(ctx, selectorQuery, updateQuery)
Getter 片段:
c := db.Collection("foo")
q, err := c.Find(ctx, bson.M{"_id": bson.M{"$in": idsImQuerying}})
if err != nil {
return nil
}
var results []Model
err = q.All(ctx, &results)
fmt.Printf("\n\n\n\n%#v\n\n\n\n", results) // this output is shown below
得到的结果:
[]Model{
Model{
ID:primitive.ObjectID{0x5a, 0xa9, 0x7a, 0x40, 0xdf, 0xe5, 0x90, 0x44, 0x49, 0xdb, 0x61, 0x4},
EntityType:"CELL",
EntityID:"4110902605985611776",
ConfigSource:ConfigSources{
Configs:[]ConfigSource(nil)
}
}
}
在图集中查看的字段:
从模型结构中删除内联后,它工作正常
type Model struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
EntityType string `bson:"entity_type,omitempty"`
EntityID string `bson:"entity_id,omitempty"`
ConfigSource ConfigSources `bson:"config_source,omitempty"`
}