带有 $lookup 查询的 MGO 管道不会附加来自 "joined" collection 的匹配文档

MGO Pipe with $lookup query won't attach matching documents from "joined" collection

我的模型

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:"-"`
    }
)

我得到collection查询:

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)

目标 :如果他们的 parent id 与 collection 中的文档之一匹配,则检索所有类别及其 parent .

问题 : 检索了所有类别但缺少 'parentlist' 加入属性

Stack:mgo 与 DB 和 golang 版本 1.8 交互

在您的聚合中,您查找 parents,它们将存储在名为 parentlist 的字段中。并且您尝试将结果解组为 Category 的切片,但是类型 Category 没有匹配 parentlist 的字段。因此在解组过程中该字段将是 "lost"。

有很多方法可以获得额外的parentlist,这个答案中详细介绍了一些可能性:

一种选择是使用这样的包装器结构:

type CategoryWithParents struct {
    Category models.Category    `bson:",inline"`
    Parents  []*models.Category `bson:"parentlist"`
}

并解组成这样的一个片段:

var results []CategoryWithParents

err := collection.Pipe(query).All(&results)

这将获得所有 parents。

如果所有类别最多可以有一个parent,可以修改聚合为$unwindparentlistParents可能是单个*model.Category 而不是切片:

type CategoryWithParents struct {
    Category       models.Category  `bson:",inline"`
    OptionalParent *models.Category `bson:"parentlist"`
}

var results []CategoryWithParents

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",
            },
        },
        {
            "$unwind": bson.M{
                "path":                       "parentlist",
                "preserveNullAndEmptyArrays": true,
            },
        },
    }

    return collection.Pipe(query).All(&results)
}