duanbei7035 2018-01-16 18:08
浏览 14
已采纳

Golang导入的字段与标准字段声明的作用不同

I'm going to try to simplify the problem down rather than bring the whole project into scope so if you have questions I'll try to update with more information.

I have 3 structs I'm working with:

type Ticket struct{
  ID bson.ObjectID `json:"id" bson:"_id"`
  InteractionIDs []bson.ObjectId `json:"interactionIds" bson:"interactionIds"`
  TicketNumber int `json:"ticketNumber" bson:"ticketNumber"`
  Active bool `json:"active" bson:"active"`
  // Other fields not included
}

type TicketInteraction struct{
  ID bson.ObjectID `json:"id" bson:"_id"`
  Active bool `json:"active" bson:"active"`
  // Other fields not included
}

type TicketLookupTicket struct{
  Ticket
  Interactions []TicketInteraction `json:"interactions" bson:"interactions"`
}

Then I have an mgo pipe that I'm working with to 'join' the two collections together

var tickets []TicketLookupTicket
c := mongodb.NewCollectionSession("tickets")
defer c.Close()

pipe := c.Session.Pipe(
  []bson.M{
    "$lookup": bson.M{
      "from": "ticketInteractions",
      "localField": "interactionIds",
      "foreignField": "_id",
      "as": "interactions",
    }
  },
)
pipe.All(&tickets)

Now tickets should be populated with the result from the database, but what is actually happening is only the interactions within each ticket has been populated. It ends up looking something like this:

[
  {
    ID: ObjectIdHex(""),
    InteractionIDs: nil,
    TicketNumber: 0,
    Active: false,
    // Other Fields, all with default values
    Interactions: []{
      {
        ID: ObjectIdHex("5a441ffea1c9800148669cc7"),
        Active: true,
        // Other Fields, with appropriate values
      }
    }
  }
]

Now if I manually declare some of the Ticket structs fields inside the TicketLookupTicket struct, those fields will start populating. Ex:

type TicketLookupTicket struct{
  Ticket
  ID bson.ObjectId `json:"id" bson:"_id"`
  TicketNumber int `json:"ticketNumber" bson:"ticketNumber"`
  Active bool `json:"active" bson:"active"`
  Interactions []TicketInteraction `json:"interactions" bson:"interactions"`
}

Now ID, TicketNumber, and Active will start populating, but the remaining fields won't.

Can anyone explain why the imported Ticket fields aren't behaving the same as the declared fields?

  • 写回答

1条回答 默认 最新

  • dongpo5264 2018-01-16 18:15
    关注

    Per the documentation, you need to add the inline tag to the field:

    type TicketLookupTicket struct{
      Ticket `bson:",inline"`
      Interactions []TicketInteraction `json:"interactions" bson:"interactions"`
    }
    

    inline Inline the field, which must be a struct or a map. Inlined structs are handled as if its fields were part of the outer struct. An inlined map causes keys that do not match any other struct field to be inserted in the map rather than being discarded as usual.

    By default, it assumes that the embedded field Ticket should be filled by an object at TicketLookupTicket.Ticket.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?