0
0
MongoDBquery~10 mins

Why change streams are needed in MongoDB - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to watch for changes in the 'orders' collection.

MongoDB
const changeStream = db.collection('orders').[1]();
Drag options to blanks, or click blank then click option'
Aupdate
Bfind
Cinsert
Dwatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using find() instead of watch() will only query existing data, not listen for changes.
Using insert() or update() are operations to modify data, not to watch changes.
2fill in blank
medium

Complete the code to handle change events from the change stream.

MongoDB
changeStream.on('[1]', next => { console.log(next); });
Drag options to blanks, or click blank then click option'
Achange
Bclose
Cerror
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' listens for errors, not data changes.
Using 'close' or 'open' are lifecycle events, not change notifications.
3fill in blank
hard

Fix the error in the code to properly start watching changes on the 'users' collection.

MongoDB
const stream = db.collection('users').[1]();
Drag options to blanks, or click blank then click option'
Alisten
Bwatch
Con
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'change' as argument to watch() causes an error.
Using listen() or subscribe() are not valid MongoDB methods.
4fill in blank
hard

Fill both blanks to filter change events for only 'insert' operations.

MongoDB
const pipeline = [ { $match: { 'operationType': [1] } } ];
const stream = db.collection('products').[2](pipeline);
Drag options to blanks, or click blank then click option'
A'insert'
B'update'
Cwatch
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' instead of 'insert' filters the wrong operation.
Using find() instead of watch() does not create a change stream.
5fill in blank
hard

Fill all three blanks to create a change stream that listens for deletes and logs the document key.

MongoDB
const pipeline = [ { $match: { 'operationType': [1] } } ];
const stream = db.collection('logs').[2](pipeline);
stream.on('[3]', change => { console.log(change.documentKey); });
Drag options to blanks, or click blank then click option'
A'delete'
Bwatch
Cchange
D'insert'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' instead of 'delete' filters the wrong operation.
Using other event names than 'change' will not capture change events.
Using methods other than watch() will not create a change stream.