db.items.find({ $expr: { $gt: [ "$A" , "$B" ] } });
Is the feature implemented in the C# driver? I’m using the Builders to build the pipeline filters, but didn’t find the appropriate methods for the $expr operator.
Hi Alexander,
The pipeline operator $expr is not yet available via the builders. There’s an open ticket to track this work: CSHARP-2084. Please feel free to upvote/watch the ticket to receive notifications on it.
You can specify BsonDocument as a work around, for example given the following document model:
{
A:1,
B:5,
C:10
}
To filter change events only for when the update of A is greater than B, use the following example:
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
var match = new BsonDocument { {
"$match", new BsonDocument { {
"$expr", new BsonDocument { {
"$gt", new BsonArray{"$updateDescription.updatedFields.A", "$fullDocument.B"}
} }
} }
} };
var pipeline = new []{ match };
var cursor = collection.Watch<ChangeStreamDocument<BsonDocument>>(pipeline, options);
var enumerator = cursor.ToEnumerable().GetEnumerator();
See also Modify Change Stream Output
Regards,
Wan