How to use IndexOf with Builders filter from mongodb c#

Viewed 205

I'm trying to filter a collection like this

Builders<myClass>.Filter.Where(d => d.Name.IndexOf(".") > length)

but getting this error :

System.InvalidOperationException: {document}{Name}.IndexOf(".") is not supported.

Any way to achieve this ?

1 Answers

you need a linq expression that can generate the following aggregation pipeline:

db.collection.aggregate([
    {
        $match: {
            $expr: {
                $gt: [{ $indexOfCP: ['$name', '.'] }, 8]
            }
        }
    }
])

but no such strongly-typed method exists in the c# driver unfortunately. as an alternative to dealing with BsonDocuments, have a look at this.

Related