简体   繁体   English

如何使用 Mongoose 从 NodeJS 中的 MongoDB Change Streams 获取 fullDocumentBeforeChange?

[英]How to get fullDocumentBeforeChange from MongoDB Change Streams in NodeJS with Mongoose?

I'm using Mongoose and MongoDB and Nodejs and I want to listen for changes to a MongoDB collection and console log the document before change and the update description.我正在使用 Mongoose、MongoDB 和 Nodejs,我想监听 MongoDB 集合的更改,并在控制台记录更改前的文档和更新描述。

The actual result: I can only log the update description.实际结果:我只能记录更新描述。

const didChangeStream = model.collection.watch({
  fullDocument: 'updateLookup',
  fullDocumentBeforeChange: 'whenAvailable'
});
didChangeStreamAlert.on('change', async (event) => {
  const {
    documentKey,
    fullDocument,
    fullDocumentBeforeChange,
    updateDescription
  } = event;
  console.log('fullDocumentBeforeChange', fullDocumentBeforeChange);
  console.log('updateDescription', updateDescription);
});

https://mongoosejs.com/docs/api.html#model_Model-watch says the options is a second argument, so must be https://mongoosejs.com/docs/api.html#model_Model-watch说选项是第二个参数,所以必须是

const didChangeStream = model.watch([], {
  fullDocument: 'updateLookup',
  fullDocumentBeforeChange: 'whenAvailable'
});

And it should work on model level.它应该在模型级别上工作。 No need to touch model.collection directly.无需直接接触model.collection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Mongodb到Mongoose的嵌套文档中获取Nodejs中的数组? - How to get an Array in Nodejs from a nested document in Mongodb through Mongoose? 如何使用 mongoose、nodejs 从 Mongodb 获取数据 - How can i get data from Mongodb using mongoose, nodejs 使用 nodejs 和 mongoose 从 mongodb 中具有 id 的对象数组中获取元素 - Get an element from an array of objects with an id in mongodb using nodejs and mongoose 使用 nodejs 和 mongoose 从具有动态嵌套级别的 mongodb 获取数据 - Get data from mongodb with dynamic nested levels using nodejs and mongoose 如何使用 mongoose 和 nodejs 从 mongodb 获取与每条记录匹配的所有数据 - How to get all data matched each record from mongodb using mongoose and nodejs 如何使用 ZCCADCDEDB567ABAE643E15DCF0974E503Z (NodeJS) 从 MongoDB 获取没有特定参数的项目 - How to get items without specific params from MongoDB using Mongoose (NodeJS) NodeJs MongoDb Mongoose-如何从多维数组中删除项目? - NodeJs MongoDb Mongoose - How to Delete item from Multi Dimensional Array? 如何使用nodejs和mongoose从mongodb集合中删除数据 - How to remove data from mongodb collection using nodejs and mongoose 如何从猫鼬获取 mongodb 版本 - How to get mongodb version from mongoose 如何在Mongoose MongoDb NodeJS中的数组中获取集合中的连接对象 - How to get the connected objects within a collection in an array in Mongoose MongoDb NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM