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 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.

    点赞 评论 复制链接分享

相关推荐