Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to watch changes on the entire database.
MongoDB
const changeStream = db.[1].watch(); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'collection' instead of 'database' to watch changes on the whole database.
Trying to call 'watch' directly on 'db' without specifying 'database'.
✗ Incorrect
To watch changes on the whole database, use db.database.watch().
2fill in blank
mediumComplete the code to listen for change events and print them.
MongoDB
changeStream.on('[1]', next => console.log(next));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' or 'data' as event names instead of 'change'.
Not using quotes around the event name.
✗ Incorrect
The event name to listen for changes is 'change'.
3fill in blank
hardFix the error in the code to start watching changes on a database named 'shopDB'.
MongoDB
const changeStream = client.db('[1]').watch();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'shop' or 'ShopDB' which do not match the exact database name.
Using underscores instead of camel case.
✗ Incorrect
The database name must match exactly, including case. Here it is 'shopDB'.
4fill in blank
hardFill both blanks to filter change events to only 'insert' operations on the database.
MongoDB
const pipeline = [ { $match: { 'operationType': [1] } } ];
const changeStream = db.database.watch(pipeline, { [2]: false }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'true' instead of 'false' for fullDocument option.
Not quoting the operationType string.
✗ Incorrect
The filter uses 'insert' for operationType, and fullDocument: false disables full document retrieval.
5fill in blank
hardFill all three blanks to close the change stream properly after use.
MongoDB
async function watchAndClose() {
const changeStream = db.database.watch();
changeStream.on('change', data => console.log(data));
await changeStream.[1]();
console.log('[2] closed');
return [3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stop' instead of 'close' to end the stream.
Returning a string instead of a boolean.
✗ Incorrect
Use close() to stop the stream, print a message, and return true to indicate success.