Last active
May 23, 2020 16:00
-
-
Save Mcdavid95/0050f43df1a19e76f1b85857c4e88975 to your computer and use it in GitHub Desktop.
change stream index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {MongoClient} = require('mongodb'); | |
async function listDatabases(client){ | |
databasesList = await client.db().admin().listDatabases(); | |
console.log("Databases:"); | |
databasesList.databases.forEach(db => console.log(` - ${db.name}`)); | |
}; | |
function closeChangeStream(timeInMs = 60000, changeStream) { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
console.log("Closing the change stream"); | |
changeStream.close(); | |
resolve(); | |
}, timeInMs) | |
}) | |
}; | |
async function monitorListingsUsingEventEmitter(client, timeInMs = 60000, pipeline = []) { | |
const collection = client.db("sample_airbnb").collection("listingsAndReviews"); | |
const changeStream = collection.watch(pipeline); | |
changeStream.on('change', (next) => { | |
console.log(next); | |
}); | |
await closeChangeStream(timeInMs, changeStream); | |
} | |
async function main(){ | |
/** | |
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster. | |
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details | |
*/ | |
const uri = 'mongodb+srv://dbUser:dbUserPassword@cluster0-jj6uu.mongodb.net/test?retryWrites=true&w=majority'; | |
const client = new MongoClient(uri); | |
try { | |
// Connect to the MongoDB cluster | |
await client.connect(); | |
const pipeline = [ | |
{ | |
'$match': { | |
'operationType': 'delete', | |
// 'fullDocument.address.country': 'Australia', | |
// 'fullDocument.address.market': 'Sydney' | |
}, | |
} | |
]; | |
// Make the appropriate DB calls | |
await listDatabases(client); | |
await monitorListingsUsingEventEmitter(client, 30000, pipeline); | |
} catch (e) { | |
console.error(e); | |
} finally { | |
await client.close(); | |
} | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment