mongodb - 从结构中的 MongoDB 获取数据非常复杂
问题描述
对于一个项目,我必须在 Go 中使用 RESTful API 查询 Mongo 数据库。
这是我的问题,我从 Mongo 得到这个结构。
{
"_id" : "ObjectID",
"id" : 1234,
"project" : {
"id" : 1,
"name" : "A project"
},
"tracker" : {
"id" : 2,
"name" : "A tracker"
},
"status" : {
"id" : 3,
"name" : "A status"
},
"priority" : {
"id" : 4,
"name" : "A priority"
},
"author" : {
"id" : 5,
"name" : "An author"
},
"assigned_to" : {
"id" : 6,
"name" : "Assigned to"
},
"category" : {
"id" : 7,
"name" : "A category"
},
"subject" : "A subject",
"description" : "A description",
"done_ratio" : 0,
"spent_hours" : 0.0,
"total_spent_hours" : 0.0,
"custom_fields" : [
{
"id" : 1,
"name" : "",
"value" : ""
},
{
"id" : 2,
"name" : "",
"multiple" : true,
"value" : [
""
]
}
],
"created_on" : "2018-12-21T15:22:57.000Z",
"updated_on" : "2018-12-26T10:58:35.000Z",
"journals" : [
{
"id" : 1,
"user" : {
"id" : 1,
"name" : ""
},
"notes" : "",
"created_on" : "2018-12-21T15:32:51Z",
"details" : [
{
"property" : "",
"name" : "",
"new_value" : ""
}
]
},
{
"id" : 2,
"user" : {
"id" : 1,
"name" : ""
},
"notes" : "",
"created_on" : "2018-12-21T15:33:54Z",
"details" : []
}
]
}
如您所见,有两个元素称为 custom_fields 和 journals,它们是对象列表。但有时它可能只是一个唯一的对象而不是一个列表。
在我的 Go API 中,我创建了以下结构:
type Custom struct {
Id int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
Multiple bool `json:"multiple" bson:"multiple"`
Value []string `json:"value" bson:"value"`
}
type Journal struct {
ID int `json:"id" bson:"id"`
User struct {
ID int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
} `json:"user" bson:"user"`
Note string `json:"notes" bson:"notes"`
Created_on string `json:"created_on" bson:"created_on"`
Details struct {
Property string `json:"property" bson:"property"`
Name string `json:"name" bson:"name"`
Old_Value string `json:"old_value" bson:"old_value"`
New_Value string `json:"new_value" bson:"new_value"`
} `json:"details" bson:"details"`
}
type MainStruct struct {
ID int `json:"id" bson:"id"`
Project struct {
ID int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
} `json:"project" bson:"project"`
Tracker struct {
ID int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
} `json:"tracker" bson:"tracker"`
Status struct {
ID int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
} `json:"status" bson:"status"`
Priority struct {
ID int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
} `json:"priority" bson:"priority"`
Author struct {
ID int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
} `json:"author" bson:"author"`
Assigned_to struct {
ID int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
} `json:"assigned_to" bson:"assigned_to"`
Subject string `json:"subject" bson:"subject"`
Description string `json:"description" bson:"description"`
Due_date time.Time `json:"due_date" bson:"due_date"`
Done_ratio int `json:"done_ratio" bson:"done_ratio"`
CustomFields []Custom `json:"custom_fields" bson:"custom_fields"`
Created_on time.Time `json:"created_on" bson:"created_on"`
Updated_on time.Time `json:"updated_on" bson:"updated_on"`
Journals []Journal `json:"journals" bson:"journals"`
}
但是当我查询 MongoDb 并获得一个作为一个 custom_field 或一个期刊的文档时,我收到以下错误:
cannot decode string into a slice
我不知道如何将一个元素变成 1 的一部分。
解决方案
推荐阅读
- javascript - 我应该如何根据 div 固定宽度减小段落字体大小
- azure - Azure Web API 设置环境变量
- ios - 为什么 Apple Health 应用程序是非活动数据源?
- java - 与其他操作员一起评估操作前和操作后的陈述
- android - 如何创建家谱结构?
- html - Autocomplete="off" 不适用于应用组合框(Chrome 69)
- android - 索引 2 处不透明部分中的非法字符颤动
- javascript - 正则表达式匹配多次
- laravel - 如何使用来自 html 的参数调用 javascript 函数?
- java - 在@CucumberOptions 中创建自定义文件报告名称