英文:
go mongodb driver and struct, find messes with uppercase and lowercase
问题
var Messages []Token
c2 := session.DB("mydatabase").C("pages")
query2 := c2.Find(bson.M{}).All(&Messages)
fmt.Print(Messages)
这是我在Mongo DB中的结构:
id_
pageUrl
token
pageId
我最初尝试的结构是这样的:
type Token struct {
PageUrl string
Token string
PageId string
}
但只有token被打印出来,可能是因为它都是小写。其他两个字段没有被检索到,因为它们包含大写字母。然后我尝试了这个:
type Token struct {
PageUrl string json:"pageUrl" bson:"pageUrl"
Token string json:"token" bson:"token"
PageId string json:"pageId" bson:"pageId"
}
那些bson
和json
是什么东西?我只是因为在互联网上看到过才加上的,但它不起作用,我仍然只得到token
字段。
使用解决方案和测试示例更新嵌套文档
我看到没有关于这个问题的帖子,所以请记住,解决方案是删除json:
和bson:
之间的空格。
此外,为了帮助那些可能想知道如何处理嵌套结构的人,这里有两个对我有效的结构:
type Token struct {
PageUrl string json:"pageUrl" bson:"pageUrl"
Token string json:"token" bson:"token"
PageId string json:"pageId" bson:"pageId"
}
type Message struct {
Sender struct {
Id string json:"id" bson:"id"
} json:"sender" bson:"sender"
Recipient struct {
Id string json:"id" bson:"id"
} json:"recipient" bson:"recipient"
Message struct {
Mid string json:"mid" bson:"mid"
Seq int json:"seq" bson:"seq"
Message string json:"text" bson:"text"
}
}
英文:
var Messages []Token
c2 := session.DB("mydatabase").C("pages")
query2 := c2.Find(bson.M{}).All(&Messages)
fmt.Print(Messages)
Here's the structure in my Mongo DB:
id_
pageUrl
token
pageId
I first tried the structure as this:
type Token struct {
PageUrl string
Token string
PageId string
}
but only the token was being printed, perhaps because it's all lowercase. The other two fields were not being retrieved because they contain uppercase. Then I tried this:
type Token struct {
PageUrl string `json: "pageUrl" bson: "pageUrl"`
Token string `json: "token" bson: "token"`
PageId string `json: "pageId" bson: "pageId"`
}
what are those bson
and json
things? I've only put it there because I've seen in the internet, but it doesn't work, I still get only the token
field
UPDATE with solution and tested example for nested documents
I've seen that there was no posts regarding this question so remember that the solution was to remove the spaces between json:
and bson:
Also, to help someone who might be wondering how to do it for nested structs, here I give two structures that worked for me:
type Token struct {
PageUrl string `json:"pageUrl" bson:"pageUrl"`
Token string `json:"token" bson:"token"`
PageId string `json:"pageId" bson:"pageId"`
}
type Message struct {
Sender struct {
Id string `json:"id" bson:"id"`
} `json:"sender" bson:"sender"`
Recipient struct {
Id string `json:"id" bson:"id"`
} `json:"recipient" bson:"recipient"`
Message struct {
Mid string `json:"mid" bson:"mid"`
Seq int `json:"seq" bson:"seq"`
Message string `json:"text" bson:"text"`
}
}
答案1
得分: 7
这些json
和bson
的东西被称为标签。
我最好的猜测是,因为Go要求变量或函数以大写字母开头,所以像json或bson这样的序列化框架要求结构体的第一个字符大写,以公开字段(以便可以看到该字段)。因此,暴露的字段名应该用标签定义(以避免限制)。
bson:
和"token"
之间的空格似乎导致了问题。
我尝试了以下代码片段,似乎工作正常。
type Token struct {
PageUrl string `json:"pageUrl" bson:"pageUrl"`
Token string `json:"token" bson:"token"`
PageId string `json:"pageId" bson:"pageId"`
}
英文:
these json
and bson
stuff is called tags
My best guess is that because Go requires a variable or function to be public by Capitalize the first character, so serialize frameworks like json or bson require the struct Capitalize its first character to expose the field(so that it could see the field). Thus the exposed field name should be defined with a tag (to avoid the restriction).
the space between bson:
and "token"
seems to have cause the problem
I tried following code snippet and seems works fine.
type Token struct {
PageUrl string `json:"pageUrl" bson:"pageUrl"`
Token string `json:"token" bson:"token"`
PageId string `json:"pageId" bson:"pageId"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论