My Models
type (
//Category implements item category in database
Category struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id"`
Name string `bson:"name" json:"name" form:"name" valid:"Required"`
IsActive bool `bson:"is_active" json:"is_active" form:"is_active" valid:"Required"`
Slug string `bson:"slug" json:"slug"`
Icon string `bson:"icon" json:"icon" form:"icon"`
SidebarIcon string `bson:"sidebar_icon" json:"sidebar_icon" form:"sidebar_icon"`
Parent bson.ObjectId `bson:"parent,omitempty" json:"parent,omitempty" form:"parent"`
CreatedAt time.Time `bson:"created_at" json:"-"`
UpdatedAt time.Time `bson:"updated_at" json:"-"`
IsDeleted bool `bson:"is_deleted" json:"-"`
}
)
My get collection query:
categories := []models.Category{}
f := func(collection *mgo.Collection) error {
query := []bson.M{
{
"$match": bson.M{
"is_deleted": bson.M{
"$ne": true,
},
},
},
{
"$sort": bson.M{
orderBy: pipeOrder,
},
},
{
"$limit": limit,
},
{
"$skip": skip,
},
{
"$lookup": bson.M{
"from": "categories",
"localField": "_id",
"foreignField": "parent",
"as": "parentlist",
},
},
}
return collection.Pipe(query).All(&categories)
Goal : retrieve all the category along with their parent if their parent id matches with one of the document inside the collection.
Issue : retrieved all the category but missing 'parentlist' joined attribute
Stack : mgo to interact with DB and golang version 1.8