v0.7.0
Menu

#Change streams

watch turns the connection into a stream of change events.

{"cmd":"watch","collection":"users","pipeline":[{"$match":{"op":"insert"}}]}

The server answers with watching:true and a token, and then sends one event per change as it happens:

{"ok":true,"token":"1a241d267370a.2","watching":true}
{"coll":"users","db":"shop","doc":{"_id":{"$oid":"6b12e67f0000c4e849f2796a"},"v":500},"id":{"$oid":"6b12e67f0000c4e849f2796a"},"op":"insert","token":"1a241d267370a.3"}

Close the socket to stop. Nothing else can be sent on that connection.

#Request

Field Meaning
collection optional. Without it, the whole database is watched
pipeline optional. A list of $match stages run on each event. Nothing but $match is allowed
resumeAfter optional. A token from an earlier event or reply

The database comes from use or a db field, and it has to exist. It needs read on the collection, or on the database when there is none. A bad $match is rejected before watching:true.

The $match filters the event document, so fields are op, db, coll, id and doc, and a path such as "doc.age" reaches into the document.

#Events

Each event has op, db, coll, id, doc and token.

op When doc
insert a document was inserted the whole document
update a document was updated the whole document after the update
replace a document was replaced the whole new document
delete a document was deleted not present
drop a collection was dropped not present
dropDatabase the database was dropped not present
createIndex, dropIndex an index was made or dropped not present

An event is made from the log entry after it was written, and its order is the order of the log. A change that failed to log makes no event. A transaction's changes are published after its commit, in order.

#Resume

A token is <run id>.<sequence>. resumeAfter replays what the server still has after that token, and then goes on live:

{"cmd":"watch","collection":"users","resumeAfter":"1a241d267370a.3"}

The server keeps the last 10,000 changes of a database, or 64 MiB of them, and only while someone is watching and for a minute after the last watcher leaves. The ring is in memory, so tokens do not survive a restart. An old, foreign or future token gets an error.

#Limits and what to expect

  • A watcher has its own queue of 10,000 events or 64 MiB. A client that falls that far behind gets a limit error and is closed. A slow watcher never blocks a writer.
  • When one write (an $out or $merge) would put more than 32 MiB of documents into events, the events after that have no doc and carry "truncated":true.
  • The server checks the socket and the user every 100 ms, so a dropped or changed user ends the stream.
  • With nobody watching, recording is off, and costs one atomic load per write.
  • There is no goodbye message. The stream ends when the process does, and a client has to reconnect and resume (within the same run) or start over.
  • watch is not allowed inside a transaction, and a follower serves it for the changes it receives.