sql >> Base de Datos >  >> NoSQL >> MongoDB

¿Cómo configurar MongoDB Change Stream 'OperationType' en el controlador C#?

Aquí hay una muestra del código que he usado para actualizar la colección Watch para recuperar "eventos" que no sean solo actualizaciones de documentos.

IMongoDatabase sandboxDB = mongoClient.GetDatabase("Sandbox");
IMongoCollection<BsonDocument> collection = sandboxDB.GetCollection<BsonDocument>("TestCollection");

//Get the whole document instead of just the changed portion
ChangeStreamOptions options = new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };

//The operationType can be one of the following: insert, update, replace, delete, invalidate
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match("{ operationType: { $in: [ 'replace', 'insert', 'update' ] } }");

var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();    //Blocks until a document is replaced, inserted or updated in the TestCollection
ChangeStreamDocument<BsonDocument> next = changeStream.Current;
enumerator.Dispose();

El argumento EmptyPiplineDefinition...Match() también podría ser:

"{ $or: [ {operationType: 'replace' }, { operationType: 'insert' }, { operationType: 'update' } ] }"

Si quisiera usar el comando $or, o

"{ operationType: /^[^d]/  }"

para lanzar un poco de expresión regular allí. Este último dice, quiero todos los tipos de operación a menos que comiencen con la letra 'd'.