- Stitch >
- MongoDB Atlas >
- Work With MongoDB
Watch for Document Changes¶
Overview¶
Stitch allows you to watch a collection and receive client-side database events whenever documents in the collection are inserted or modified. This enables you to develop real-time and collaborative applications without the overhead and complexity of alternatives such as long polling.
Stitch uses MongoDB change streams on watched collections to listen for changes and broadcast them to any client applications that are watching documents in the collection at the time of the event. Stitch supports watching any change on a collection and watching changes that match a certain filter.
Change Stream Limitations
Stitch opens a single MongoDB change stream for each watched collection and limits the total number of open change streams on each linked cluster across all Stitch apps based on the cluster’s size. See change stream limitations for more information.
Setup¶
- JavaScript SDK
- Android SDK
- iOS SDK
To use the following code snippets in a JavaScript project, you must first do the following:
Import Stitch Dependencies¶
– or –
Instantiate a MongoDB Remote Collection Handle¶
To use the following code snippets in an Android project, you must first do the following:
Set Up Your Project¶
Follow the steps in the Set up a MongoDB Mobile Project guide.
Note
For more details on setting up your Android app to use Stitch, refer to Build a Mobile App with Sync or Build a Local-Only Mobile App.
Import Stitch Dependencies¶
For CRUD operations on a remote MongoDB collection, you will use one
or more of the following import
statements:
To Sync documents between a remote and a local MongoDB instance, you will also need to import the following packages:
Important
If you use Sync, be sure to add .sync()
before every remote
database call in the examples below, as in the following:
To perform CRUD operations only on the local (on-device) database, import the following packages:
Instantiate a MongoDB Collection Handle¶
To use the following code snippets in an iOS project, you must first do the following:
Set Up Your Project¶
Follow the steps in the Set up a MongoDB Mobile Project guide.
Import Stitch Dependencies¶
Initialize the MongoDB Stitch iOS SDK¶
Instantiate a MongoDB Collection Handle¶
Watch Snippets¶
Watch a Collection¶
- JavaScript SDK
- Android SDK
- iOS SDK
You can open a stream of changes made to a collection by calling collection.watch() with no argument. Whenever the watched collection changes, the stream’s event handler fires with a ChangeEvent object as its only argument.
You can open a stream of changes made to a collection by calling RemoteMongoCollection.watch(). Whenever the watched collection changes, the AsyncChangeStream will emit an event.
You can open a stream of changes made to a collection by calling RemoteMongoCollection.watch(delegate:). Whenever the watched collection changes, the passed ChangeStreamDelegate will receive the associated ChangeEvent.
Watch a Collection with a Filter¶
- JavaScript SDK
- Android SDK
- iOS SDK
You can open a stream of changes that match a filter by calling collection.watch(delegate:) with a $match expression as the argument. Whenever the watched collection changes and the ChangeEvent matches the provided $match expression, the stream’s event handler fires with the ChangeEvent object as its only argument.
The $match expression is similar to the $match expression passed when configuring a database trigger.
You can open a stream of changes that match a filter by calling
RemoteMongoCollection.watchWithFilter()
with a $match expression
as the matchFilter
argument. Whenever the watched collection changes
and the change event matches the provided $match expression, the
AsyncChangeStream
will emit an event.
The $match expression is similar to the $match expression passed when configuring a database trigger.
You can open a stream of changes that match a filter by calling
RemoteMongoCollection.watch(matchFilter:delegate:)
with a $match expression
as the matchFilter
argument. Whenever the watched collection changes
and the change event matches the provided $match expression, the passed
ChangeStreamDelegate
will receive the ChangeEvent.
The $match expression is similar to the $match expression passed when configuring a database trigger.