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?