Bson regex deserialization

Hi, one of our endpoints accepts raw mongo query and I have encountered issue with regexes.

The filter definition is created by

var bson = BsonSerializer.Deserialize<BsonDocument>(query);
var definition = new BsonDocumentFilterDefinition<CellDo>(bson);

The query looks like this:

{
   "description": {
      "$regex": /\w*\.a\b/
   }
}

But when the query is passed serializer throws an exception saying JSON reader expected a string but found '/\\w*\\.a\\b/'.

I tried to wrap regular expression in strings - the expression is treated as string so doesn’t really help and I have also wrapped the expression into RegEx call, but it just thrown exception JSON reader expected a string but found 'RegEx'.

The queries looks like this:

{
    "description": {
        "$regex": RegEx("/\w*\.a\b/")
    }
}
{
    "description": {
        "$regex": RegEx(/\w*\.a\b/)
    }
}

I wonder if this is a bug because similar calls with ISODate instead are working fine.

We are currently using mongo from docker image mongo:4.2.6-bionic and MongoDB.Driver 2.10.3

I just had the same problem today. Basically, I wanted to dynamically replace a part of my string (PLACEHOLDER) with either exact or regex match. Exact match would replace it so that it becomes ’ “chocolate” ', or regex ’ "{$regex: “chocolate”} " '.

Instead what is returned is ‘/chocolate/’ after deserialization. This causes a problem for some of the names with special characters. In Mongo Compass I can use ’ "{$regex: “chocolate”} " ’ directly, but for some reason deserialization does not work with it.

I used the following deserialization method in C#:
var something = MongoDB.Bson.Serialization.BsonSerializer.Deserialize(stringFilter);