登录
首页 >  Golang >  Go问答

MongoDB 聚合管道 $match 阶段的起草存在问题

来源:stackoverflow

时间:2024-02-16 15:57:26 144浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《MongoDB 聚合管道 $match 阶段的起草存在问题》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

我正在尝试使用 go.mongodb.org/mongo-driver/mongo 库以 golang 语言进行查询(下面我附加了纯 mongodb 查询的工作代码),下面是 golang 查询代码。我无法让 matchstage 正常工作,我尝试了很多变体,我确信我只是非常不专心或者只是不明白

如何同时使用 $match$expr$and$lte 来制作正确的 matchstage

func (r *Mongo) ChatHistory(ctx context.Context, chatID string, f *Filter) ([]*Message, error) {
    matchStage := bson.D{
        primitive.E{
            Key: "$match",
            Value: bson.D{
                primitive.E{Key: "$expr", Value: bson.D{
                    primitive.E{Key: "$and", Value: bson.A{
                        bson.D{
                            primitive.E{Key: "$lte", Value: bson.D{
                                primitive.E{
                                    Key:   "$create_date",
                                    Value: f.Date, // int64
                                },
                            }},
                        },
                    }},
                }},
            },
        },
    }
    sortStage := bson.D{
        {
            Key: "$sort", Value: bson.D{
                primitive.E{Key: "create_date", Value: -1},
            },
        },
    }
    limitStage := bson.D{primitive.E{Key: "$limit", Value: f.Count}}

    cursor, err := r.colMessage.Aggregate(ctx, mongo.Pipeline{matchStage, sortStage, limitStage})
    if err != nil {
        l.Error().Err(err).Msg("failed find")
        return nil, err
    }

    var res []*Message
    if err = cursor.All(ctx, &res); err != nil {
        l.Error().Err(err).Msg("failed find all documents")
        return nil, err
    }

    if err = cursor.Close(ctx); err != nil {
        l.Error().Err(err).Msg("failed close cursor")
        return nil, err
    }

    return res, nil
}

错误: (invalidpipelineoperator) 无法识别的表达式 '$create_date'

mongodb 游乐场链接


正确答案


$lte 的值必须是数组而不是文档:

matchstage := bson.d{
    primitive.e{
        key: "$match",
        value: bson.d{
            primitive.e{key: "$expr", value: bson.d{
                primitive.e{key: "$and", value: bson.a{
                    bson.d{
                        primitive.e{key: "$lte", value: bson.a{
                            "$create_date",
                            f.date, // int64
                        }},
                    },
                }},
            }},
        },
    },
}

另请注意,您可以从复合文字中省略 primitive.e 类型:

matchstage := bson.d{
    {
        key: "$match",
        value: bson.d{
            {key: "$expr", value: bson.d{
                {key: "$and", value: bson.a{
                    bson.d{
                        {key: "$lte", value: bson.a{
                            "$create_date",
                            f.date, // int64
                        }},
                    },
                }},
            }},
        },
    },
}

但是请注意,你在 mongo playground 上的表达是不正确的。引用自 doc一个>:

$match 接受一个指定查询条件的文档。查询语法与 读取操作查询语法 ...

使用$expr时,必须使用$eq,例如:

matchstage := bson.d{
    {
        key: "$match",
        value: bson.d{
            {key: "$expr", value: bson.d{
                {key: "$and", value: bson.a{
                    bson.d{
                        {key: "$eq", value: bson.a{
                            "$chat_id",
                            chatid,
                        }},
                    },
                    bson.d{
                        {key: "$lte", value: bson.a{
                            "$create_date",
                            f.date, // int64
                        }},
                    },
                }},
            }},
        },
    },
}

在这里尝试一下:https://mongoplayground.net/p/sbejd-fyhjl

您应该在$match中使用普通的查询文档。

查看这个等效的、更简单的解决方案:

matchstage := bson.d{
    {
        key: "$match",
        value: bson.d{
            {key: "chat_id", value: chatid},
            {key: "create_date", value: bson.d{
                {
                    key:   "$lte",
                    value: f.date, // int64
                }},
            },
        },
    },
}

如果您使用 bson.m 而不是 bson.d,甚至会简单得多:

matchStage := bson.M{
    "$match": bson.M{
        "chat_id":     chatID,
        "create_date": bson.M{"$lte": f.Date},
    },
}

当然,在最后一种情况下,您不能使用 mongo.pipeline 作为管道,但 []any[]bson.m 也可以。

以上就是《MongoDB 聚合管道 $match 阶段的起草存在问题》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>