0
0
MongoDBquery~10 mins

Change streams on databases in MongoDB - Interactive Code Practice

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

Complete 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'
Adatabase
Bwatch
CwatchStream
Dcollection
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'.
2fill in blank
medium

Complete 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'
Aevent
Bnext
Cdata
Dchange
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' or 'data' as event names instead of 'change'.
Not using quotes around the event name.
3fill in blank
hard

Fix 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'
Ashop
BShopDB
CshopDB
Dshop_db
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'shop' or 'ShopDB' which do not match the exact database name.
Using underscores instead of camel case.
4fill in blank
hard

Fill 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'
A'insert'
BfullDocument
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'true' instead of 'false' for fullDocument option.
Not quoting the operationType string.
5fill in blank
hard

Fill 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'
Aclose
BChange stream
Ctrue
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stop' instead of 'close' to end the stream.
Returning a string instead of a boolean.