Mongo C# driver - Contains Filter

Mr767267 picture Mr767267 · Aug 21, 2015 · Viewed 26.7k times · Source

I am using the latest version of Mongo C# driver which uses a lot of Async and builder pattern. Which is nice. I am trying to convert SQL where clauses into Mongo FilterDefinition object.

Any idea how to handle "contains"?
like:

where x contains 'ABC'

Answer

Ofir picture Ofir · Apr 6, 2016

In order to achieve that in V2 API, use the `Filter.Regex':

var collection = db.GetCollection<BsonDocument>("collection");

var filter = Builders<BsonDocument>.Filter.Regex("fieldName", new BsonRegularExpression(".*fieldValue.*"));

var data = await (await coll.FindAsync<BsonDocument>(filter).ConfigureAwait(false)).ToListAsync();

//continue process data