Connection.prototype.watch()
Parameters
- [pipeline] «Array»
- [options] «Object» passed without changes to the MongoDB driver’s
Db#watch()
function
Returns:
- «ChangeStream» mongoose-specific change stream wrapper, inherits from EventEmitter
Watches the entire underlying database for changes. Similar to Model.watch()
.
This function does not trigger any middleware. In particular, it does not trigger aggregate middleware.
The ChangeStream object is an event emitter that emits the following events
- ‘change’: A change occurred, see below example
- ‘error’: An unrecoverable error occurred. In particular, change streams currently error out if they lose connection to the replica set primary. Follow this GitHub issue for updates.
- ‘end’: Emitted if the underlying stream is closed
- ‘close’: Emitted if the underlying stream is closed
Example:
const User = conn.model('User', new Schema({ name: String }));
const changeStream = conn.watch().on('change', data => console.log(data));
// Triggers a 'change' event on the change stream.
await User.create({ name: 'test' });
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .