使用另一个集合中的值过滤 MongoDB 集合

发布于 2025-01-13 06:31:40 字数 714 浏览 3 评论 0原文

我是 MongoDB 初学者。我有这些模式:

const postSchema = new mongoose.Schema(
    {
        content: { type: String, required: true, index: "text" },
        author: { type: ObjectId, ref: "User", required: true, index: true }
    }
);
const muteWordSchema = new mongoose.Schema({
    word: { type: String, trim: true, required: true },
    match: { type: String, enum: ["exact", "contains", "startsWith", "endsWith"], required: true },
});

我想做的是:

  1. 获取所有帖子
  2. 获取所有静音单词
  3. 将静音单词转换为相应的正则表达式。例如,{ word: "test", match: "startsWith" } 将变为 /(^|\s)test/g,依此类推。
  4. 过滤掉与这些转换后的正则表达式匹配的所有帖子。

如何使用聚合管道实现这一目标?

I am a MongoDB beginner. I have these schemas:

const postSchema = new mongoose.Schema(
    {
        content: { type: String, required: true, index: "text" },
        author: { type: ObjectId, ref: "User", required: true, index: true }
    }
);
const muteWordSchema = new mongoose.Schema({
    word: { type: String, trim: true, required: true },
    match: { type: String, enum: ["exact", "contains", "startsWith", "endsWith"], required: true },
});

What I want to do is:

  1. Fetch all posts
  2. Fetch all muted words
  3. Transform the muted words into corresponding regular expressions. For example, { word: "test", match: "startsWith" } will become /(^|\s)test/g, and so on.
  4. Filter out all posts that match these transformed regular expressions.

How can I achieve this using aggregation pipelines?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2025-01-20 06:31:40

我没有得到答案,但无论如何我都会发布我找到的解决方案,因此对于其他遇到类似问题的人来说这将很有用。

db.posts.aggregate([
    {
        $lookup: {
            from: "mutedwords",
            pipeline: [
                {
                    $project: {
                        _id: 0,
                        regEx: {
                            $switch: {
                                branches: [
                                    {
                                        case: {
                                            $eq: [ "$match", "startsWith" ]
                                        },
                                        then: {
                                            $concat: [
                                                "\\W+",
                                                "$word",
                                                ".*"
                                            ]
                                        }
                                    },
                                    {
                                        case: {
                                            $eq: [ "$match", "endsWith" ]
                                        },
                                        then: {
                                            $concat: [
                                                "\w*",
                                                "$word",
                                                "(\\W+|$)"
                                            ]
                                        }
                                    },
                                    {
                                        case: {
                                            $eq: [ "$match", "exact" ]
                                        },
                                        then: {
                                            $concat: [
                                                "\\W+",
                                                "$word",
                                                "(\\W+|$)"
                                            ]
                                        }
                                    }
                                ],
                                default: "$word"
                            }
                        }
                    }
                },
                {
                    $group: {
                        _id: undefined,
                        result: {
                            $addToSet: "$regEx"
                        }
                    }
                }
            ],
            as: "mutedWords"
        }
    },
    {
        $addFields: {
            mutedWords: {
                $arrayElemAt: ["$mutedWords.result", 0]
            }
        }
    },
    {
        $match: {
            $expr: {
                $eq: [
                    {
                        $filter: {
                            input: "$mutedWords",
                            cond: {
                                $regexMatch: {
                                    input: "$content",
                                    regex: "$this",
                                    options: "i"
                                }
                            }
                        }
                    },
                    []
                ]
            }
        }
    },
    {
        $unset: "mutedWords"
    }
]);

I didn't get an answer, but I will post the solution I found anyway, so it will be useful for anyone else having a similar problem.

db.posts.aggregate([
    {
        $lookup: {
            from: "mutedwords",
            pipeline: [
                {
                    $project: {
                        _id: 0,
                        regEx: {
                            $switch: {
                                branches: [
                                    {
                                        case: {
                                            $eq: [ "$match", "startsWith" ]
                                        },
                                        then: {
                                            $concat: [
                                                "\\W+",
                                                "$word",
                                                ".*"
                                            ]
                                        }
                                    },
                                    {
                                        case: {
                                            $eq: [ "$match", "endsWith" ]
                                        },
                                        then: {
                                            $concat: [
                                                "\w*",
                                                "$word",
                                                "(\\W+|$)"
                                            ]
                                        }
                                    },
                                    {
                                        case: {
                                            $eq: [ "$match", "exact" ]
                                        },
                                        then: {
                                            $concat: [
                                                "\\W+",
                                                "$word",
                                                "(\\W+|$)"
                                            ]
                                        }
                                    }
                                ],
                                default: "$word"
                            }
                        }
                    }
                },
                {
                    $group: {
                        _id: undefined,
                        result: {
                            $addToSet: "$regEx"
                        }
                    }
                }
            ],
            as: "mutedWords"
        }
    },
    {
        $addFields: {
            mutedWords: {
                $arrayElemAt: ["$mutedWords.result", 0]
            }
        }
    },
    {
        $match: {
            $expr: {
                $eq: [
                    {
                        $filter: {
                            input: "$mutedWords",
                            cond: {
                                $regexMatch: {
                                    input: "$content",
                                    regex: "$this",
                                    options: "i"
                                }
                            }
                        }
                    },
                    []
                ]
            }
        }
    },
    {
        $unset: "mutedWords"
    }
]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文