MGO return bson 字段而不是 json 字段
MGO return bson field instead of json field
在mgo中执行管道时使用了bson名称。
结构:
type Training struct {
Id bson.ObjectId `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
Description string `json:"description"`
Level *TrainingLevel `json:"level"`
Preworks []bson.ObjectId `json:"preworks"`
PrePostTests []bson.ObjectId `json:"preposttests" bson:"preposttests"`
TrainingEvaluations []bson.ObjectId `json:"training_evaluations" bson:"training_evaluations"`
TrainerEvaluations []bson.ObjectId `json:"trainer_evaluations" bson:"trainer_evaluations"`
AppCompanyId bson.ObjectId `json:"app_company_id" bson:"app_company_id"`
Company *Company `json:"company"`
}
函数:
func (this *TrainingClass) GetAllTraining() (interface{}, error) {
if !this.tokenInfo.IsAllowed(this.c) {
return nil, tlib.NewTError(common.Error_NoAccess, "You don't have the right!")
}
sess, db := GetDB()
defer sess.Close()
pipeline := []bson.M{
{"$match": bson.M{
"app_company_id": this.tokenInfo.AppCompanyId}},
{"$lookup": bson.M{
"from": "trainingbatch",
"localField": "_id",
"foreignField": "training._id",
"as": "trainingbatches"}},
}
resp := []bson.M{}
db.C(common.C_TRAINING).Pipe(pipeline).All(&resp)
return bson.M{"data": resp}, nil
}
Json 结果:
{
"data": [
{
"_id": "5995a749dbcfbe4e8cc31378",
"app_company_id": "58b24756e65bd121f6b1a923",
"description": "Description First Training",
"name": "First Training",
"trainingbatches": [
{
"_id": "5995a74adbcfbe4e8cc31379",
"app_company_id": "58b24756e65bd121f6b1a923",
"company": {
"_id": "58b24756e65bd121f6b1a923",
"address": "",
"app_company_id": "58b24756e65bd121f6b1a923",
"fullname": "",
"name": "Tandem",
"phone": ""
},
}
]
}
]
}
如您所见,生成了字段 _id 而不是 id。如果我使用 find 或 findId,则不会发生这种情况。有什么方法可以继续使用 json 字段,无论查询是什么?
您读取结果的方式,它不知道 JSON 字段名称是什么。为了让它使用这些标签,它实际上必须反序列化到指定标签的结构中。当你这样做时:
resp := []bson.M{}
db.C(common.C_TRAINING).Pipe(pipeline).All(&resp)
您明确告诉 mgo
到 return BSON 结果。您传入的对象(bson.M
的一片)上没有 json 标签。为了控制对 JSON 的序列化,您必须将带有 JSON 标记的结构传递给 All
:
resp := []Training
db.C(common.C_TRAINING).Pipe(pipeline).All(&resp)
在mgo中执行管道时使用了bson名称。 结构:
type Training struct {
Id bson.ObjectId `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
Description string `json:"description"`
Level *TrainingLevel `json:"level"`
Preworks []bson.ObjectId `json:"preworks"`
PrePostTests []bson.ObjectId `json:"preposttests" bson:"preposttests"`
TrainingEvaluations []bson.ObjectId `json:"training_evaluations" bson:"training_evaluations"`
TrainerEvaluations []bson.ObjectId `json:"trainer_evaluations" bson:"trainer_evaluations"`
AppCompanyId bson.ObjectId `json:"app_company_id" bson:"app_company_id"`
Company *Company `json:"company"`
}
函数:
func (this *TrainingClass) GetAllTraining() (interface{}, error) {
if !this.tokenInfo.IsAllowed(this.c) {
return nil, tlib.NewTError(common.Error_NoAccess, "You don't have the right!")
}
sess, db := GetDB()
defer sess.Close()
pipeline := []bson.M{
{"$match": bson.M{
"app_company_id": this.tokenInfo.AppCompanyId}},
{"$lookup": bson.M{
"from": "trainingbatch",
"localField": "_id",
"foreignField": "training._id",
"as": "trainingbatches"}},
}
resp := []bson.M{}
db.C(common.C_TRAINING).Pipe(pipeline).All(&resp)
return bson.M{"data": resp}, nil
}
Json 结果:
{
"data": [
{
"_id": "5995a749dbcfbe4e8cc31378",
"app_company_id": "58b24756e65bd121f6b1a923",
"description": "Description First Training",
"name": "First Training",
"trainingbatches": [
{
"_id": "5995a74adbcfbe4e8cc31379",
"app_company_id": "58b24756e65bd121f6b1a923",
"company": {
"_id": "58b24756e65bd121f6b1a923",
"address": "",
"app_company_id": "58b24756e65bd121f6b1a923",
"fullname": "",
"name": "Tandem",
"phone": ""
},
}
]
}
]
}
如您所见,生成了字段 _id 而不是 id。如果我使用 find 或 findId,则不会发生这种情况。有什么方法可以继续使用 json 字段,无论查询是什么?
您读取结果的方式,它不知道 JSON 字段名称是什么。为了让它使用这些标签,它实际上必须反序列化到指定标签的结构中。当你这样做时:
resp := []bson.M{}
db.C(common.C_TRAINING).Pipe(pipeline).All(&resp)
您明确告诉 mgo
到 return BSON 结果。您传入的对象(bson.M
的一片)上没有 json 标签。为了控制对 JSON 的序列化,您必须将带有 JSON 标记的结构传递给 All
:
resp := []Training
db.C(common.C_TRAINING).Pipe(pipeline).All(&resp)