dongrenzheng1619 2017-08-17 15:22
浏览 53
已采纳

MGO返回bson字段而不是json字段

The bson name is used when performing pipe in mgo. Struct :

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"`
}

Function :

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 result :

{
  "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": ""
          },
        }
      ]
    }
  ]
}

As you can see field _id is generated instead of id. That's not happen if I use find or findId. Is there any way to keep using json field no matter what's the query?

  • 写回答

1条回答 默认 最新

  • dpjw67160 2017-08-17 16:20
    关注

    The way you're reading the result, it has no idea what the JSON field names are. In order for it to use those tags, it must actually deserialize into the struct where the tags have been specified. When you do:

        resp := []bson.M{}
        db.C(common.C_TRAINING).Pipe(pipeline).All(&resp)
    

    You're explicitly telling mgo to return BSON results. The object you pass in (a slice of bson.M) has no json tags on it. In order to control the serialization to JSON, you must pass a struct with the JSON tags specified to All:

        resp := []Training
        db.C(common.C_TRAINING).Pipe(pipeline).All(&resp)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab