Actually, I am working on a change stream in Node.js (Express.js). I got stuck in the below situation.
here i have made Database Connetion with mongoDB using mongoose
async function main() {
const uri = process.env.DATABASE;
const client = mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
reconnectInterval: 500, // Reconnect every 500m
})
// Connect to the MongoDB cluster
await client
.then(() => console.log('DB Connected'))
.catch(er => console.log(er));
const pipeline = [
{
'$match': {
$or: [{ operationType: 'insert' }, { operationType: 'update' }]
}
}
];
await monitorListingsUsingEventEmitter(client, 30000,pipeline);
}
main().catch(console.error);
this is function for change stream
async function monitorListingsUsingEventEmitter(client, timeInMs = 60000, pipeline = []) {
const changeStream = await members.watch(pipeline);
changeStream.on('change', async (next) => {
switch (next.operationType) {
case 'insert':
console.log('an insert happened...', "uni_ID: ", next.fullDocument);
break;
case 'update':
console.log('an update happened...', next.fullDocument);
//
//getting undefined in above line because there is no property name 'fullDocument' in object next
//
break;
case 'delete':
console.log('a delete happened...');
break;
default:
break;
}
});
overall my question is that :
I need a full document object that is getting updated without any query
I got my answer. I just checked https://www.mongodb.com/docs/manual/reference/method/db.collection.watch/.
Actully i have use
const changeStream = await members.watch(pipeline);
above statement should be like this. its working now
const changeStream = await members.watch(pipeline, { fullDocument: "updateLookup" }
//this actual answer
Thanks everyone for not answering :) finally i did