MongoDB Go驱动程序无法正确解组嵌套文档

问题描述 投票:0回答:1

我有一个setter和getter,它对集合中的特定字段进行操作。 setter可以正常工作,并且文档已按预期进行更新,但是getter不能正确返回填充的结构。 我在做什么错?


作为Go结构的Collection


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)

字母片段:

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)
        }
    }
}

在Atlas中查看的字段:

screenshot of atlas document viewer

mongodb go bson mongo-go
1个回答
0
投票

一旦我从Model结构中删除了内联程序,它就可以正常工作

     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"`
    }
© www.soinside.com 2019 - 2024. All rights reserved.