0
0
MongoDBquery~10 mins

Change streams on collections 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 'users' collection.

MongoDB
const changeStream = db.collection('users').[1]();
Drag options to blanks, or click blank then click option'
Aobserve
Bwatch
Clisten
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'listen' or 'observe' which are not valid MongoDB methods.
Trying to use 'subscribe' which is not a MongoDB collection method.
2fill in blank
medium

Complete the code to specify the pipeline to filter change events for inserts only.

MongoDB
const pipeline = [ { $match: { 'operationType': [1] } } ];
Drag options to blanks, or click blank then click option'
A'insert'
B'update'
C'delete'
D'replace'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' which watches for modifications, not inserts.
Using 'delete' which watches for removals.
3fill in blank
hard

Fix the error in the code to correctly iterate over change events.

MongoDB
for await (const change of changeStream.[1]()) {
  console.log(change);
}
Drag options to blanks, or click blank then click option'
Anext
Biterator
Ccursor
Dstream
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' which is a method on the cursor, not the changeStream itself.
Using 'stream' or 'iterator' which are not valid methods here.
4fill in blank
hard

Fill both blanks to create a change stream with a pipeline filtering for deletes and fullDocument lookup.

MongoDB
const changeStream = db.collection('orders').watch([1], { fullDocument: [2] });
Drag options to blanks, or click blank then click option'
A[{ $match: { operationType: 'delete' } }]
Btrue
Cfalse
D[{ $match: { operationType: 'insert' } }]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operationType in the pipeline.
Setting fullDocument to false which disables full document lookup.
5fill in blank
hard

Fill all three blanks to handle change events and close the stream on error.

MongoDB
try {
  for await (const change of changeStream.[1]()) {
    console.log(change.[2]);
  }
} catch (error) {
  console.error(error);
  changeStream.[3]();
}
Drag options to blanks, or click blank then click option'
Acursor
BfullDocument
Cclose
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'cursor()' for iteration.
Accessing 'document' instead of 'fullDocument'.
Not closing the stream on error.