MONGOC_CLIENT_WATCH(3)

πŸ’… MONGOC_CLIENT_WATCH πŸ’…

okay bestie, this is the libmongoc page for mongoc_client_watch β€” literally the glow-up shortcut for making a change stream ✨


🌸 SYNOPSIS 🌸

mongoc_change_stream_t*
mongoc_client_watch (mongoc_client_t *client,
                     const bson_t *pipeline,
                     const bson_t *opts);

πŸ¦‹ DESCRIPTION πŸ¦‹

So this is a little helper function that spins up a change stream for you πŸŽ€ and honestly? Use this instead of hand-rolling a raw aggregation to make a change stream. It's the preferred way, no cap. It's giving "let the library carry the bags."

It uses the read preference and read concern of the client β€” and if the change stream ever needs to re-establish its connection, it'll use that same read preference again. That reconnecting situation can pop off if the change stream runs into a resumable error. 😭

⚠️ WARNING: a change stream is only supported with majority read concern. That's the vibe check it has to pass, periodt.

This function is considered a retryable read operation. On a transient error (a network error, errors from replica set failover, etc. etc.) the operation gets safely retried once. But if retryreads is false in the URI (peep mongoc_uri_t), then the retry behavior does not apply. πŸ™ˆ


✨ PARAMETERS ✨

  • db β€” a mongoc_client_t specifying the client which the change stream listens to. πŸ‘‘
  • pipeline β€” a bson_t representing an aggregation pipeline appended to the change stream. It may be an empty document, so no pressure. πŸ’–
  • opts β€” a bson_t containing change stream options.
πŸŽ€ Changed in version 2.0.0: batchSize of 0 is applied to the aggregate command. 0 was previously ignored. (iconic behavior change, honestly)

opts may be NULL or a BSON document with additional command options:

  • batchSize β€” an int32 requesting a limit of documents returned in each server reply. If it's positive, the batchSize is applied to both aggregate and getMore commands. If it's 0, the batchSize is only applied to the aggregate command (useful to request an immediate cursor without significant server-side work β€” see Aggregate Data Specifying Batch Size). If it's omitted or negative, the value is ignored and server defaults are used (see Cursor Batches for a description of server defaults). πŸ’…
  • resumeAfter β€” a Document representing the logical starting point of the change stream. The result of mongoc_change_stream_get_resume_token() or the _id field of any change received from a change stream can be used here. This option is mutually exclusive with startAfter and startAtOperationTime. 🚫
  • startAfter β€” a Document representing the logical starting point of the change stream. Unlike resumeAfter, this one can resume notifications after an "invalidate" event. The result of mongoc_change_stream_get_resume_token() or the _id field of any change received from a change stream can be used here. This option is mutually exclusive with resumeAfter and startAtOperationTime. πŸ”₯
  • startAtOperationTime β€” a Timestamp. The change stream only provides changes that occurred at or after the specified timestamp. Any command run against the server will return an operation time that can be used here. This option is mutually exclusive with resumeAfter and startAfter. ⭐
  • maxAwaitTimeMS β€” an int64 representing the maximum amount of time a call to mongoc_change_stream_next() will block waiting for data.
  • fullDocument β€” an optional UTF-8 string. Set this option to "default", "updateLookup", "whenAvailable", or "required". If unset, the string "default" is assumed. Set this option to "updateLookup" to direct the change stream cursor to lookup the most current majority-committed version of the document associated to an update change stream event. πŸ‘€
  • fullDocumentBeforeChange β€” an optional UTF-8 string. Set this option to "whenAvailable", "required", or "off". When unset, the default value is "off". Similar to "fullDocument", but returns the value of the document before the associated change. πŸ•°οΈ
  • showExpandedEvents β€” set to true to return an expanded list of change stream events. Available only on MongoDB versions >=6.0. πŸ™Œ
  • comment β€” a bson_value_t specifying the comment to attach to this command. The comment will appear in log messages, profiler output, and currentOp output. Only string values are supported prior to MongoDB 4.4. πŸ“

πŸ’– RETURNS πŸ’–

A newly allocated mongoc_change_stream_t which must be freed with mongoc_change_stream_destroy() when no longer in use. The returned mongoc_change_stream_t is never NULL β€” yes bestie, never, we love a guaranteed pointer. πŸ’€ If there is an error, it can be retrieved with mongoc_change_stream_error_document(), and subsequent calls to mongoc_change_stream_next() will return false.

πŸ‘€ SEE ALSO πŸ‘€

  • mongoc_database_watch()
  • mongoc_collection_watch()

🫢 AUTHOR 🫢

MongoDB, Inc

2009-present, MongoDB, Inc.


and that's the whole page, bestie β€” libmongoc version 2.5.6, dated Oct 09, 2026, signed off with love πŸ’œ go forth and watch those changes πŸ¦‹βœ¨

πŸ“¦ where this page comes from