Class: ChangeStream

ChangeStream

new ChangeStream(changeDomain, pipeline, options){ChangeStream}

Creates a new Change Stream instance. Normally created using Collection.watch().

Name Type Default Description
changeDomain Db | Collection

The collection against which to create the change stream

pipeline Array

An array of aggregation pipeline stages through which to pass change stream documents

options object null optional

Optional settings

Name Type Default Description
fullDocument string 'default' optional

Allowed values: ‘default’, ‘updateLookup’. When set to ‘updateLookup’, the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.

maxAwaitTimeMS number optional

The maximum amount of time for the server to wait on new documents to satisfy a change stream query

resumeAfter object null optional

Specifies the logical starting point for the new change stream. This should be the _id field from a previously returned change stream document.

batchSize number null optional

The number of documents to return per batch. See aggregation documentation.

collation object null optional

Specify collation settings for operation. See aggregation documentation.

readPreference ReadPreference null optional

The read preference. Defaults to the read preference of the database or collection. See read preference documentation.

Since:
  • 3.0.0
Fires:
Returns:
ChangeStream instance.

Methods

close(callback){Promise}

Close the Change Stream

Name Type Description
callback ChangeStream~resultCallback optional

The result callback.

Returns:
Promise if no callback passed

hasNext(callback){Promise}

Check if there is any document still available in the Change Stream

Name Type Description
callback ChangeStream~resultCallback optional

The result callback.

Throws:
Returns:
Promise if no callback passed

isClosed(){boolean}

Is the cursor closed

next(callback){Promise}

Get the next available document from the Change Stream, returns null if no more documents are available.

Name Type Description
callback ChangeStream~resultCallback optional

The result callback.

Throws:
Returns:
Promise if no callback passed

This method will cause a stream in flowing mode to stop emitting data events. Any data that becomes available will remain in the internal buffer.

pipe(destination, options){null}

This method pulls all the data out of a readable stream, and writes it to the supplied destination, automatically managing the flow so that the destination is not overwhelmed by a fast readable stream.

Name Type Description
destination Writable

The destination for writing data

options object optional

Pipe options

resume(){null}

This method will cause the readable stream to resume emitting data events.

Return a modified Readable stream including a possible transform method.

Name Type Default Description
options object null optional

Optional settings.

Name Type Default Description
transform function null optional

A transformation method applied to each document emitted by the stream.

unpipe(destination){null}

This method will remove the hooks set up for a previous pipe() call.

Name Type Description
destination Writable optional

The destination for writing data

Type Definitions

resultCallback(error, result)

The callback format for results

Name Type Description
error MongoError

An error instance representing the error during the execution.

result object | null

The result object if the command was executed successfully.

Events

Fired for each new matching change in the specified namespace. Attaching a change event listener to a Change Stream will switch the stream into flowing mode. Data will then be passed as soon as it is available.

Type:
  • object

Change stream close event

Type:
  • null

Change stream end event

Type:
  • null

Fired when the stream encounters an error.

Type:
  • Error