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 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号