Given the following JSON, I'm trying to get all documents that match "myemail@domain.com". I need a case insensitive filter. I thought if I added i to the value it would perform the search but no luck.
I'm very new to MongoDB so excuse me if this is a simple issue.
{
"_id" : ObjectId("5aecc56d2a7e7a408c9767e3"),
"Children" : [
{
"Email" : "MyEmail@Domain.com",
"Children" : [
{
"Name" : "Some name",
"Value" : "some value",
}
]
}
]
}
var email = "myemail@domain.com";
var filter = Builders<Subscription>.Filter.Eq("Children.Email", $"/{email}/i");
return await context.Subscriptions.Find(filter).ToListAsync();
There's a dedicated class BsonRegularExpression
which can be used as a parameter of Regex
method. Simple Eq
will try to perform regular equality comparison here. Try this:
var email = "myemail@domain.com";
var filter = Builders<Subscription>.Filter.Regex("Children.Email", new BsonRegularExpression(email, "i"));
return await context.Subscriptions.Find(filter).ToListAsync();