Evaluation query expressions in C# driver

104 views
Skip to first unread message

Alexander Seipel

unread,
May 23, 2018, 3:35:00 AM5/23/18
to mongodb-user
Hi,

I stumbled upon the $expr feature in 3.6. I want to use it in conjunction with change streams to only receive updates if property A of the updated document is greater than property B.

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.

Wan Bachtiar

unread,
Jul 24, 2018, 12:01:18 AM7/24/18
to mongodb-user

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

Reply all
Reply to author
Forward
0 new messages