I was using pipe function in mgo to retrieve data. My data structs are as follows.
type Company struct {
Id bson.ObjectId `bson:"_id,omitempty"`
CompanyName string
Slug string
CompanyUsers []CompanyUser
}
type CompanyUser struct {
UserName string
Email string
FullName string
}
I needed to check whether a given "UserName" is present under "CompanyUsers" in a "Company" with a given "Slug".
Both Slug and UserName are provided by the User.
Using Pipe function I successfully done the search, but the data is returned with an empty CompanyUsers array.
My query is as follows:
var companyResults []Company
pipeline := []bson.M{
{"$match": bson.M{"slug": slug}},
{"$unwind": "$companyusers"},
{"$match": bson.M{
"companyusers.username": username,
}},
}
err := c.Pipe(pipeline).All(&companyResults)
This provides me a search result as follows:
[{ObjectIdHex("573aa0fddd731711c94830ca") MyCompany companyslug [] }]
None of the data in CompanyUsers are retrieved. How can I solve this error?