英文:
Go nested object with mgo adapter
问题
我正在进行一个基于MongoDB数据结构的项目。我们存储在数据库中的对象的样子如下:
{
"_id" : ObjectId("567a877df1c7720bea7c2f51"),
"username" : "dog",
"type" : "regular",
"data" : {
"full" : {
"xx" : "xx",
"xx" : "xx",
"yy" : {
"xx" : "test"
},
"yy" : {
"xx" : {
}
}
}
}
}
我们在Golang中使用的结构如下:
type User struct {
Id bson.ObjectId `bson:"_id,omitempty"`
username string
Type string
data struct {
full struct {
xx string `json:"xx"`
xx string `json:"xx"`
xxx struct{} `json:"xx"`
yy struct {
}
}
}
}
问题是第一个属性可以正常填充数据,但对象内部的对象无法正常工作。
我们用的是常规的代码,就像在MGO文档中看到的那样来提取数据。
err = collection.Find(bson.M{"username": username}).One(&user)
有没有特定的方法以这种方式获取数据?
英文:
I'm working on a project that based on MongoDB data structure. Our objects that stored inside the database looks like this:
{
"_id" : ObjectId("567a877df1c7720bea7c2f51"),
"username" : "dog",
"type" : "regular",
"data" : {
"full" : {
"xx" : "xx",
"xx" : "xx",
"yy" : {
"xx" : "test"
},
"yy" : {
"xx" : {
}
}
}
}
And the struct that we working on with Golang looks like this:
type User struct {
Id bson.ObjectId `bson:"_id,omitempty"`
username string
Type string
data struct {
full struct {
xx string `json:"xx"`
xx string `json:"xx"`
xxx struct{} `json:"xx"`
yy struct {
}
}
}
}
The thing is that the first properties gets fill with data without any problem but the objects inside the object are not working.
Our code to pull data is the regular code as we saw in the MGO documentation.
err = collection.Find(bson.M{"username": username}).One(&user)
Is there any specific way to fetch the data in that way?
答案1
得分: 2
我只返回翻译好的部分,不回答关于翻译的问题。
我只是手写了这个。但是你必须记住关于大写名字字段和结构内部的json
形式属性。
type User struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Username string
Type string
Data struct {
Full struct {
Xx string `json:"xx"`
Xx string `json:"xx"`
Xxx struct{} `json:"xx"`
Yy struct {
} `json:"yy"`
} `json:"full"`
} `json:"data"`
}
编辑:
另一个工作示例
Go中的结构体
type Event struct {
EvL []struct {
BaV int `json:"basicV"`
ChI int `json:"chann"`
DaU int `json:"dataU"`
} `json:"eventL"`
ST int `json:"send.dat_ts"`
}
以下是将上述结构写入数据库的示例
{
"_id": ObjectId("560d422e65f47eef8a118cbd"),
"evl": [
{
"bav": 255,
"chi": 14,
"dau": 0
}
],
"st": 5
}
英文:
I wrote this jus from hand. But You must remember about capitalize name field, and property json
form inside structure.
type User struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Username string
Type string
Data struct { // Data nor data
Full struct { // Full nor full
Xx string `json:"xx"` // Xx nor xx
Xx string `json:"xx"`
Xxx struct{} `json:"xx"`
Yy struct { // Yy nor yy
}`json:"yy"`
} `json:"full"`
} `json:"data"`
}
EDIT:
Another works example
Structure in go
type Event struct{
EvL []struct {
BaV int `json:"basicV"`
ChI int `json:"chann"`
DaU int `json:"dataU"`
} `json:"eventL"`
ST int `json:"send.dat_ts"`
}
Below how to looks above structure wrote to DB
{ "_id" :
ObjectId("560d422e65f47eef8a118cbd"),
"evl" :
[
{
"bav" : 255,
"chi" : 14,
"dau" : 0,
],
"st" : 5
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论