Reputation: 1593
Here is document structure
{
"_id" : ObjectId("6284f3eceae4fd4c3cfebb39"),
"Codes" : [
"Code 1",
"Code 2",
"Code 3"
]
}
Also i have mongo query which works as expected
{ "Codes" : { "$elemMatch" : { "$regex" : /code/, $options: 'si' } } }
But when i tried to write C# code with mongodb driver
var filter = Builders<MyClass>.Filter
.ElemMatch(x => x.Codes,
Builders<string>.Filter.Regex(c => c, new BsonRegularExpression($"/^{codeFilter}/is")));
and execute this filter definition i've got an exception
System.InvalidOperationException: 'Unable to determine the serialization information for c => c.'
Where is my mistake? How can i create filter definition to check string array items with $regex
operator?
Upvotes: 0
Views: 317
Reputation: 4857
You created wrong generic type for regex builder:
Builders<MyClass>.Filter.ElemMatch(
x => x.Codes,
Builders<Codes>.Filter.Regex(
c => c.Code,
new BsonRegularExpression($"/code/", "si")));
where the entities structure are:
private class MyClass
{
public Codes[] Codes { get; set; }
}
private class Codes
{
public string Code { get; set; }
}
Upvotes: 1