我正在尝试使用 MGO 在类型的映射中从我的 MongoDB 接收数据[]map[string]interface{}
我的 JSON 看起来像这样 -
{
"_id":"string",
"brandId":123,
"category":{
"television":[
{
"cat":"T1",
"subCategory":[
{
"subCat":"T1A TV",
"warrantyPeriod":6
}
],
"warrantyPeriod":12
},
{
"cat":"T2",
"subCategory":[
{
"subCat":"T2A",
"warrantyPeriod":18
},
{
"subCat":"T2B",
"warrantyPeriod":9
}
],
"warrantyPeriod":15
},
{
"cat":"T3",
"subCategory":[
{
"subCat":"T3A",
"warrantyPeriod":3
},
{
"subCat":"T3B",
"warrantyPeriod":5
},
{
"subCat":"T3C",
"warrantyPeriod":7
},
{
"subCat":"T3D",
"warrantyPeriod":11
}
],
"warrantyPeriod":4
}
],
"television_warrantyPeriod":24
},
"title":"BrandName"
}
理想情况下,我会传入类别名称,即“电视”cat和subCat值,这些值可能是可选的。
例如像这样的东西 -
{
"categorySlug": "television",
"brandId": "123",
"model": "T2"
}
在这种情况下,如果没有指定 T2A 或 T2B,我希望找到“15”,这是 T2 的保修期值。
我的查询功能如下所示 -
var data map[string]string
err := json.NewDecoder(r.Body).Decode(&data)
log.Println(err)
var buffer bytes.Buffer
buffer.WriteString("category.")
buffer.WriteString(data["categorySlug"])
brandId, _ := strconv.Atoi(data["brandId"])
concernedbrandandcategory := database.GetMappedFields("Brands", bson.M{"brandId": brandId, buffer.String(): bson.M{"$exists": true}}, bson.M{buffer.String(): 1})
categorymap := concernedbrandandcategory[0]
log.Println(categorymap["category"]["television"], reflect.TypeOf(categorymap))
我只是无法理解这个嵌套结构,它有时返回一个地图,有时返回一个界面!任何帮助将不胜感激!
慕村9548890
相关分类