Advertisement
exapsy

mongodb_streams

May 20th, 2024
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { MongoClient } = require('mongodb');
  2.  
  3. async function listenToChanges() {
  4.   const uri = 'mongodb://yourMongoDBURI';
  5.   const client = new MongoClient(uri);
  6.  
  7.   try {
  8.     await client.connect();
  9.     const database = client.db('yourDatabase');
  10.     const collection = database.collection('yourCollection');
  11.  
  12.     const changeStream = collection.watch();
  13.  
  14.     changeStream.on('change', (change) => {
  15.       console.log('Change detected:', change);
  16.     });
  17.   } finally {
  18.     // Keep the connection open to listen for changes
  19.   }
  20. }
  21.  
  22. listenToChanges().catch(console.error);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement