0

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?

1 Answer 1

1

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; }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.