gpt4 book ai didi

mongodb - 将结构与 mgo 结果匹配

转载 作者:数据小太阳 更新时间:2023-10-29 03:15:03 24 4
gpt4 key购买 nike

我的本​​地 mongodb 中有以下文档:

_id 25dd9d29-efd5-4b4e-8af0-360c49fdba31
name Reykjavik
initialDiseaseColouring blue

在我的代码中,我设置了如下城市结构:

type City struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string
InitialDiseaseColouring string
}

我正在使用

查询它
result := City{}
collection.Find(bson.M{"name":"Reykjavik"}).One(&result)

当我尝试访问 initialDiseaseColouring 属性时,它不存在

这是我打印出来的结果:

{ObjectIdHex("32356464396432392d656664352d346234652d386166302d333630633439666462613331") 雷克雅未克 }

有人知道为什么吗?

我正在关注 https://gist.github.com/border/3489566 上的示例

最佳答案

默认情况下,bson 编解码器使用小写的字段名称作为键。使用字段标记指定不同的键:

type City struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string
InitialDiseaseColouring string `bson:"initialDiseaseColouring"`
}

添加字段标签会将键从“initialdiseasecolouring”更改为“initialDiseaseColouring”。

关于mongodb - 将结构与 mgo 结果匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38342365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com