使用另一个集合中的值过滤 MongoDB 集合
我是 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 },
});
我想做的是:
- 获取所有帖子
- 获取所有静音单词
- 将静音单词转换为相应的正则表达式。例如,
{ word: "test", match: "startsWith" }
将变为/(^|\s)test/g
,依此类推。 - 过滤掉与这些转换后的正则表达式匹配的所有帖子。
如何使用聚合管道实现这一目标?
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:
- Fetch all posts
- Fetch all muted words
- Transform the muted words into corresponding regular expressions. For example,
{ word: "test", match: "startsWith" }
will become/(^|\s)test/g
, and so on. - Filter out all posts that match these transformed regular expressions.
How can I achieve this using aggregation pipelines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有得到答案,但无论如何我都会发布我找到的解决方案,因此对于其他遇到类似问题的人来说这将很有用。
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.