[CSHARP-1944] Filtering with BsonRegularExpressions appears to be broken in 2.4.3 Created: 15/Mar/17  Updated: 27/Oct/23  Resolved: 17/Mar/17

Status: Closed
Project: C# Driver
Component/s: BSON, Linq
Affects Version/s: 2.4.3
Fix Version/s: None

Type: Bug Priority: Blocker - P1
Reporter: John Cleve Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Win 7, Visual Studio 2013



 Description   

In 2.4.2 the following worked when defining a FilterDefinition:

filterDefinition = builder.Eq(
                        x => x.Domain, 
                        new BsonRegularExpression(string.Format("^{0}$" : "{0}", query.Domain), "i"));

However, this appears to be broken in 2.4.3



 Comments   
Comment by John Cleve [ 16/Mar/17 ]

Hi Robert,

It's still not clear why our existing implementation as described above works in 2.4.2 but not in 2.4.3, but I've updated our code to utilize the Regex filter per your recommendation and all seems well. Thank you for your prompt reply and attention to this concern. We can close this ticket.

Best regards,

John

Comment by Robert Stam [ 16/Mar/17 ]

Any updates?

I think we can close this ticket as "Works as designed".

Comment by Robert Stam [ 15/Mar/17 ]

It's not using Eq that is returning exact (albeit case-insensitive) matches. It's anchoring the regular expression with "^" at the front and "$" at the end.

While I still recommend that you use Regex instead of Eq when matching a string field against a regular expression, I'm also unable to reproduce any problem with 2.4.3.

The following code:

var builder = Builders<C>.Filter;
var filter = builder.Eq(x => x.Domain, new BsonRegularExpression("^abc$", "i"));
var registry = BsonSerializer.SerializerRegistry;
var serializer = registry.GetSerializer<C>();
var rendered = filter.Render(serializer, registry).ToJson();
Console.WriteLine(rendered);

Outputs

{ "Domain" : /^abc$/i }

with either 2.4.2 or 2.4.3.

Comment by John Cleve [ 15/Mar/17 ]

We originally used the Regex filter, but found the Eq filter worked better for our needs. For example, while using the the Regex filter, a search for "abc" would return "abc", "0abc1", "123abc", etc. While the Eq filter would return exact matches. I guess the main issue for us is that this was working quite well for us in 2.4.2, but no longer. Thanks Robert.

Comment by John Cleve [ 15/Mar/17 ]

A clearer example might look like this:

var builder = Builders<myClass>.Filter
filterDefinition = builder.Eq(x => x.myProperty, new BsonRegularExpression("^someString$", "i"));

This filter works in 2.4.2, but not in 2.4.3

Comment by Robert Stam [ 15/Mar/17 ]

Can you check your code for me?

It doesn't compile for me. Seems like there is a parenthesis missing somewhere.

Note: I'm guessing

"^{0}$" : "{0}"

was meant to be just

"^{0}$"

Comment by Robert Stam [ 15/Mar/17 ]

Assuming x.Domain is a string I think the intended way to express that filter is:

filterDefinition = builder.Regex(
    x => x.Domain, 
    new BsonRegularExpression(string.Format("^{0}$", query.Domain), "i"));

In other words, you want to use a Regex filter, not an Eq filter.

Comment by Robert Stam [ 15/Mar/17 ]

What is the data type of x.Domain?

Generated at Wed Feb 07 21:41:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.