-replSet option. mongod --replSet rs0Connect to the MongoDB instance using the MongoDB shellInitiate the replica set by running: rs.initiate()
const MongoClient = require('mongodb').MongoClient;
const uri = "your_mongodb_uri";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function monitorChanges() {
try {
await client.connect();
const database = client.db("yourDatabase");
const collection = database.collection("yourCollection");
const changeStream = collection.watch();
changeStream.on("change", (next) => {
console.log(next);
// handle change
});
} catch (e) {
console.error(e);
}
}
monitorChanges();
https://www.mongodb.com/developer/products/mongodb/real-time-data-javascript/