0
0
MongoDBquery~5 mins

Resume tokens for reliability in MongoDB

Choose your learning style9 modes available
Introduction
Resume tokens help you continue reading data from where you left off without missing or repeating anything.
When you want to watch changes in a database and not lose track if your app restarts.
When you read a large list of updates and want to pause and continue later.
When your network connection drops and you want to resume listening without starting over.
When you process data streams and need to avoid duplicates or gaps.
When building reliable real-time apps that react to database changes.
Syntax
MongoDB
const changeStream = collection.watch([], { resumeAfter: resumeToken });
The resumeToken is a special marker from a previous change stream event.
Use resumeAfter option to start watching changes from that marker.
Examples
Start watching changes from now without a resume token.
MongoDB
const changeStream = collection.watch();
Resume watching changes from the last saved token.
MongoDB
const changeStream = collection.watch([], { resumeAfter: lastResumeToken });
Save the resume token from each change event to resume later.
MongoDB
changeStream.on('change', (change) => {
  lastResumeToken = change._id;
});
Sample Program
This program connects to MongoDB, watches changes on a collection, saves the resume token from each change, and stops after 10 seconds.
MongoDB
const { MongoClient } = require('mongodb');

async function watchWithResume() {
  const client = new MongoClient('mongodb://localhost:27017');
  await client.connect();
  const db = client.db('testdb');
  const collection = db.collection('testcol');

  let resumeToken = null;

  // Start watching changes
  const changeStream = collection.watch([], resumeToken ? { resumeAfter: resumeToken } : {});

  changeStream.on('change', (change) => {
    console.log('Change detected:', change);
    resumeToken = change._id; // Save token to resume later
  });

  // Simulate running for 10 seconds
  setTimeout(async () => {
    await changeStream.close();
    await client.close();
    console.log('Stopped watching changes.');
  }, 10000);
}

watchWithResume();
OutputSuccess
Important Notes
Resume tokens are unique IDs for each change event.
If you lose the token, you must start watching from the beginning or current time.
Always save the token after processing each change to avoid missing data.
Summary
Resume tokens let you continue watching database changes reliably.
Use the resumeAfter option with the saved token to resume.
Save tokens after each event to avoid missing or repeating changes.