MongoDB匹配计算值

发布于 2025-01-22 06:17:24 字数 1734 浏览 1 评论 0原文

我已经创建了一个聚合查询,但是由于某种原因,它似乎对在聚合管道中创建的自定义字段不起作用。

return this.repository.mongo().aggregate([
    {
        $match: { q1_avg: { $regex: baseQuery['value'], $options: 'i' } },  // NOT WORKING
    },
    {
        $group: {
            _id: '$product_sku',
            id: { $first: "$_id" },
            product_name: { $first: '$product_name' },
            product_category: { $first: '$product_category' },
            product_sku: { $first: '$product_sku' },
            q1_cnt: { $sum: 1 },
            q1_votes: { $push: "$final_rating" }
        },
    },
    {
        $facet: {
            pagination: [ { $count: 'total' } ],
            data: [
                {
                    $project: {
                        _id: 1,
                        id: 1,
                        product_name: 1,
                        product_category: 1,
                        product_sku: 1,
                        q1_cnt: 1,
                        q1_votes: {
                            $filter: {
                                input: '$q1_votes',
                                as: 'item',
                                cond: { $ne: ['$$item', null] }
                            }
                        },
                    },
                },
                {
                    $set: {
                        q1_avg: { $round: [ { $avg: '$q1_votes' }, 2 ] },
                    }
                },
                { $unset: ['q1_votes'] },
                { $skip: skip },
                { $limit: limit },
                { $sort: sortList }
            ]
        }
    },
    { $unwind : "$pagination" },
]).next(); 

Q1_AVG值是一个整数,据我所知,Regex仅与字符串一起使用。可能是原因

I've created an aggregate query but for some reason it doesn't seem to work for custom fields created in the aggregation pipeline.

return this.repository.mongo().aggregate([
    {
        $match: { q1_avg: { $regex: baseQuery['value'], $options: 'i' } },  // NOT WORKING
    },
    {
        $group: {
            _id: '$product_sku',
            id: { $first: "$_id" },
            product_name: { $first: '$product_name' },
            product_category: { $first: '$product_category' },
            product_sku: { $first: '$product_sku' },
            q1_cnt: { $sum: 1 },
            q1_votes: { $push: "$final_rating" }
        },
    },
    {
        $facet: {
            pagination: [ { $count: 'total' } ],
            data: [
                {
                    $project: {
                        _id: 1,
                        id: 1,
                        product_name: 1,
                        product_category: 1,
                        product_sku: 1,
                        q1_cnt: 1,
                        q1_votes: {
                            $filter: {
                                input: '$q1_votes',
                                as: 'item',
                                cond: { $ne: ['$item', null] }
                            }
                        },
                    },
                },
                {
                    $set: {
                        q1_avg: { $round: [ { $avg: '$q1_votes' }, 2 ] },
                    }
                },
                { $unset: ['q1_votes'] },
                { $skip: skip },
                { $limit: limit },
                { $sort: sortList }
            ]
        }
    },
    { $unwind : "$pagination" },
]).next(); 

q1_avg value is an integer and as far as I know, regex only works with strings. Could that be the reason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文