dongrenzheng1619 2017-08-17 15:22
浏览 54


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{}

    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{}

    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错