javascript - 如何通过changestream监听mongodb数据库中的特定字段?
问题描述
我的 mongodb 数据库名称users上有一个集合,每个用户都包含一个字段名称posts。当用户添加新帖子或更新现有帖子并从套接字服务器获取更新值并将其发送回前端时,我想获取该帖子字段的实时更改数据。
单个用户文档看起来像这样
[
{
"_id" : "613fb31a52ef63541d036fcd",
"posts"["613fb31a52ef63541d036fdf", "613fb31a52ef63541d036ff1"],
"mobiles" : [ "01751150412"],
"languages" : [ "English", "Bangla"],
}
]
起初,我尝试过 const userChangeStream = connection.collection("users").watch()
这给了我用户集合的所有字段,甚至没有改变。
然后我按照这个stackoverflow问题更新了我的代码
const filter =
[
{
$match:
{
$and:
[
{ "updateDescription.updatedFields.posts": { $exists: true } },
{ operationType: "update" }
]
}
}
]
const options = { fullDocument: 'updateLookup' };
const postsChangeStream = connection.collection("users").watch(filter,options);
postsChangeStream.on("change", (change) => {
console.log('change on', change);
});
现在,在添加该帖子字段时我没有得到任何响应。
解决方案
推荐阅读
- r - 如何将给定的样本分成 2 个子样本;正面和负面的?
- flutter - 颤振我想在每个项目上放置复选框
- python - 从时间序列数据计算转移矩阵的有效方法是什么?
- swift - 如何在 isComplate 端的“真实”价值中总结货币价值?
- google-apps-script - 如何使用 Google Apps 脚本计算百分比
- vba - 自动化 Word 宏代码以用连字符替换“^=”等运算符字符串?
- netlogo - 如何在每设定数量的滴答声中杀死和再生海龟
- python - 用 json.loads(r.text) 过滤掉 json
- ruby-on-rails - 如何在 ruby on rails 上绘制 2D 矢量场?
- performance - Visual Studio Code C++ Intellisense 性能问题