[英]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.