Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB Go驱动程序无法正确解组嵌套文档_Mongodb_Go_Mongo Go - Fatal编程技术网

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

MongoDB Go驱动程序无法正确解组嵌套文档,mongodb,go,mongo-go,Mongodb,Go,Mongo Go,我有一个setter和getter,它对集合中的特定字段进行操作。setter工作正常,文档按预期更新,但是getter不能正确返回填充的结构我做错了什么? 作为Go结构的集合: 类型模型结构{ ID primitive.ObjectID`bson:“\u ID,省略为空”` EntityType字符串`bson:“实体类型,省略为空”` EntityID字符串`bson:“实体id,省略为空”` ConfigSource ConfigSources`bson:“配置源,内联,省略空”` }

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


作为Go结构的集合


类型模型结构{
ID primitive.ObjectID`bson:“\u ID,省略为空”`
EntityType字符串`bson:“实体类型,省略为空”`
EntityID字符串`bson:“实体id,省略为空”`
ConfigSource ConfigSources`bson:“配置源,内联,省略空”`
}
类型ConfigSources结构{
Configs[]ConfigSource`bson:“配置,忽略空”`
}
类型ConfigSource结构{
小时整数'bson:“小时”`
源字符串`bson:“源”`
}
Setter代码段:

[]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)
        }
    }
}
cfg:=ConfigSources{
配置:[]配置源{
{
时间:1,,
来源:“小时1小时来源”,
},
{
时间:2,,
来源:“小时2小时来源”,
},
},
}
c:=db.集合(“foo”)
selectorQuery:=bson.M{“entity\u id”:entityId}
updateQuery:=bson.M{“$set”:bson.M{“config_source”:configName}
结果,err:=c.updateName(ctx、selectorQuery、updateQuery)
Getter片段:

[]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)
        }
    }
}
c:=db.Collection(“foo”)
q、 err:=c.Find(ctx,bson.M{“\u id”:bson.M{“$in”:idsImQuerying}})
如果出错!=零{
归零
}
var结果[]模型
err=q.All(ctx和结果)
fmt.Printf(“\n\n\n%\v\n\n\n\n”,results)//此输出如下所示
获得的结果:

[]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中查看的字段:

[]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"`
    }

哦,谢谢!这起作用了。我似乎误解了
inline
标记。内联使表示忽略了
config\u source
键,直接返回
configs
,这似乎不符合模型。我之前曾将内联视为一种支持存储嵌套结构的方法。这为我澄清了一切。