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 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM