MongoDB Change Stream Typed Watch

React to collection changes with a typed MongoDB change stream, filtering operations, resuming by token after restart, and cleaning up on close.

TypescriptNodeData Fetching

Code

typescriptsnippet
37 lines
import { MongoClient, ChangeStreamDocument } from 'mongodb';

interface OrderDoc {
  _id: object;
  status: 'pending' | 'shipped' | 'delivered';
  total: number;
}

const client = new MongoClient(process.env.MONGO_URI ?? 'mongodb://127.0.0.1:27017');
await client.connect();

const orders = client.db('shop').collection<OrderDoc>('orders');

let resumeToken: string | null = null;

const stream = orders.watch(
  [{ $match: { operationType: { $in: ['insert', 'update'] } } }],
  { fullDocument: 'updateLookup' },
);

stream.on('change', (event: ChangeStreamDocument<OrderDoc>) => {
  if (event.operationType === 'insert' || event.operationType === 'update') {
    const doc = event.fullDocument;
    console.log('order changed:', doc?._id, 'status', doc?.status);
  }
  // 断点续传:记录最新令牌,重启后从该位置继续
  resumeToken = JSON.stringify(stream.resumeToken);
});

stream.on('error', async (err) => {
  console.error('stream failed:', err.message);
  await stream.close().catch(() => {});
});

await orders.insertOne({ status: 'pending', total: 1200 } as OrderDoc);

setTimeout(() => stream.close().then(() => client.close()), 2000);

How the snippet works

Every asynchronous step is awaited, so rejections surface where the call happens. From mongodb it pulls MongoClient, ChangeStreamDocument — the imports name exactly which library surface the snippet touches.

Top-level bindings client, orders, stream hold the demo state — nothing sits inside a function scope.

Environment variables MONGO_URI drive configuration, so one artifact serves every environment.

Emits through the .error level methods with structured payloads, so downstream transports can route by level and field.

Watch out for

  • Plan for an install step: mongodb is a real dependency, resolved by the package manager.

Using it

The demo output is labeled (order changed:, stream failed:), so each line of output maps back to the operation it came from.

Key statements

await stream.close().catch(() => {});
setTimeout(() => stream.close().then(() => client.close()), 2000);
const client = new MongoClient(process.env.MONGO_URI ?? 'mongodb://127.0.0.1:27017');

FAQ

What does this need from the package registry?
These are real dependencies: `mongodb`. Without it the snippet will not load.