watch() (Watch Collection Changes)

Last updated: 2026-10-09

watch() (Watch Collection Changes)

watch() monitors a collection for changes and returns a Change Stream, e.g. `db.users.watch([{ $match: { 'operationType': 'insert' } }])`. Every insert, update, delete, or replace emits a change event. It requires a replica set or sharded cluster — standalone servers do not support it. Typical uses include real-time sync, cache invalidation, and audit logging.

CategoryIndex & Admin
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.<collection>.watch(<pipeline>, { fullDocument: 'updateLookup' })

⚙️ Parameters

pipelineOptional. Aggregation pipeline filtering change events.
fullDocumentOptional. 'updateLookup' attaches the full document to update events.

Returns:A Change Stream cursor to iterate or listen on.

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// watch: listen for inserts into users
const stream = db.users.watch([
  { $match: { 'operationType': 'insert' } }
])
stream.forEach(ev => print('New user: ' + ev.fullDocument.name))

Output:

New user: Alice
New user: Bob

📌 Notes

📚 Related Tutorials

❓ FAQ

QDoes watch work on a standalone MongoDB?
ANo — watch() needs a replica set or sharded cluster; it errors on a standalone deployment.
QCan watch listen only to inserts?
AYes — filter with a pipeline: db.users.watch([{$match: {'operationType': 'insert'}}]).
QWhat are watch() typical use cases?
AReal-time sync, cache invalidation, audit logging, and event-driven flows that must react to data changes.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏