【问题标题】:mongodb changestream “pipeline” not working nodejsmongodb changestream“管道”不起作用nodejs
【发布时间】:2020-12-01 07:06:39
【问题描述】:

我有以下更改流,但它没有功能,一旦我使用 mongo compass 更新,就不会记录更改。

var pipeline = [
  { $match: { _id: ObjectId(id) } }
];
try {
  const collection = client.db("mydb").collection("shop");
  const changeStream = collection.watch(pipeline);
  changeStream.on('change', (next) => {
    //console.log(next);
    console.log('changed')
  }, err => {
    console.log(err);
  });
} catch (err) {
  console.log(err)
}

【问题讨论】:

  • 您没有更改具有正确 _id 的文档,或者您在错误的集合或数据库中更改它。
  • 我正处于测试阶段...我实际上是从 Mongo Compass 复制 id
  • 你复制错了或者你在错误的集合/数据库上操作。
  • 我有另一个不使用 _id 的功能管道,它的功能......我没有复制错误的 _id

标签: node.js mongodb mongodb-query aggregation-framework changestream


【解决方案1】:

是不是你通常不更新集合中文档的_id的问题?如果出于某种原因,您正在更新 _id,那么问题可能在于您如何引用 $match。这对我有用:

const pipeline01 = [
  { $match: { 'updateDescription.updatedFields.fieldIamInterestedIn': { $ne: undefined } } },
  { $project: { 'fullDocument._id': 1, 'fullDocument.anotherFieldIamInterestedIn': 1 } },
];
theCollectionIamWatching.watch(pipeline01, { fullDocument: 'updateLookup' }).on('change', async (data) => {
// do the thing I want to do using data.fullDocument
});

【讨论】:

    猜你喜欢
    • 2021-04-27
    • 2019-07-20
    • 2021-01-04
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2020-04-13
    相关资源
    最近更新 更多