[CSHARP-1950] Allow string or regular expressions in values for $in with string Created: 23/Mar/17  Updated: 07/Jul/22  Resolved: 26/May/22

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

Type: Bug Priority: Major - P3
Reporter: Ssemi Seol Assignee: Robert Stam
Resolution: Done Votes: 2
Labels: triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

using mongo csharp driver 2.4.3


Issue Links:
Duplicate
is duplicated by CSHARP-1906 Case insensitive search using $in cla... Closed
Epic Link: CSHARP-3615

 Description   

When trying to parse the following query:

var f = Builders<collectionFoo>.Filter;
var query = f.In("product", some.Products)
                    & f.Gte("version", "1.0.0")
                    & f.In("version", new List<BsonRegularExpression>() { new BsonRegularExpression("/^1.2.0/"), new BsonRegularExpression("/^1.1.3/")  });

another ways

new BsonArray(some.versions.Select(x => new BsonRegularExpression(x)))
new BsonArray(some.versions.Select(x => new RegEx(x)))

I wanted the following results.

{ "$match" : { "product" : { "$in" : [1, 2, 3, 4] }, "version" : { "$gte" : "1.0.0", "$in" : [/^1.2.0/, /^1.1.3/, .... ] }

But the result is ...

{ "$match" : { "product" : { "$in" : [1, 2, 3, 4] }, "version" : { "$gte" : "1.0.0", "$in" : ["/^1.2.0/", "/^1.1.3/", ....] }

using BsonRegularExpression in `$in` command

https://docs.mongodb.com/manual/reference/operator/query/in/
The $in operator can specify matching values using regular expressions of the form /pattern/. You cannot use $regex operator expressions inside an $in.

I want to use regex in '$in' command..



 Comments   
Comment by Githook User [ 26/May/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-1950: Allow string or regular expressions in values for $in with string.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/417388744a71b625fcd86e68926b0eec1bf906f5

Comment by Damian Jaszczurowski [ 30/Mar/19 ]

what's the status of this?  I'm using 2.7.0 and I experience the same problem. It wouldn't be a big problem but I heave heavy documents and when I manually deserialize it from BsonDocument this makes entire process less performant.

Comment by Kevin Fairs [ 21/Dec/17 ]

What is the situation with this? We have extensive code using this pattern and running OK using the v2.4.0 driver, but unable to upgrade as broken in later versions. I see it is still not resolved in v2.5.0. Will a fix be imminent or do we have to re-write our code due to a breaking change?
Thanks

Comment by Robert Stam [ 23/Mar/17 ]

Thanks for reporting this. This is a side effect of the driver using the field serializer to serialize values. In order to do that, an attempt is made to convert the supplied value to the type of the field. In this case, a BsonRegularExpression can be converted to a string, which results in the regular expressions being serialized as strings.

A minimal reproduction is:

public class C
{
    public string S { get; set; }
}
 
public static class Program
{
    public static void Main(string[] args)
    {
        var builder = Builders<C>.Filter;
        var filter = builder.In("S", new object[] { new BsonRegularExpression("^a"), "bcd" });
        var registry = BsonSerializer.SerializerRegistry;
        var serializer = registry.GetSerializer<C>();
        var rendered = filter.Render(serializer, registry).ToJson();
    }
}

The desired query is:

{ S : { $in : [ /^a/, "bcd" ] } }

But the actual (incorrect) result is:

{ S : { $in : [ "/^a/", "bcd" ] } }

And a similar scenario using LINQ is:

var values = new object[] { new BsonRegularExpression("^a"), "bcd" };
var query = collection.AsQueryable().Where(c => values.Contains(c.S));
var aggregate = query.ToString();

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