I use mongoose to watch mongodb.
And use this docs example.
https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-change-streams?tabs=javascript
Like this:
start(): void {
let defaultSchemaChangeStream = this._alertDocumentModel.watch([
{$match: {"operationType": {$in: ["insert"]}}},
{$project: {"_id": 1, "fullDocument": 1, "ns": 1, "documentKey": 1}}
],
{fullDocument: "updateLookup"});
console.log(`defaultSchemaChangeStream= ${JSON.stringify(defaultSchemaChangeStream)}`
defaultSchemaChangeStream.on('change', async (data: any) => {
console.log(`alert collection has changed data is`, data, `data.operationType`, data.operationType)
console.log(new Date(), `[Server]Alert have insert`, data.fullDocument)
this._messageSender.sendMessages([
{body: data.fullDocument}
]).then(r => console.log(`[Server]server send service bus`, r))
})
}
And run nodejs , get error message:
MongoError: Match stage must include constraints on "operationType" to include "insert", "update", and "replace".
But I just want "insert" change streams , how to do that??