I've got a Visual Studio C# project that's using the MongoDB driver v2.0, and I am attempting to update it to use driver v2.3.0.
There's a section of code which builds a list of IMongoQuery entries based on the presence of various search fields, e.g.
var queryList = new List<IMongoQuery>();
if (!string.IsNullOrEmpty(searchField1))
queryList.Add(Query.Matches(sKey1, searchField1));
...
if (!string.IsNullOrEmpty(searchFieldN))
queryList.Add(Query.Matches(sKeyN, searchFieldN));
How do I convert this to the new FilterDefinitionBuilder syntax? I don't see a similar Add() method in its interface.
UPDATE:
Here's what I'm currently doing, and it is UGLY! Please let me know if there's a better way to do this.
var builder = Builders<BsonDocument>.Filter;
FilterDefinition<BsonDocument> filter = null;
// do this for each search field
if (!string.IsNullOrEmpty(searchField1))
{
if (filter == null)
filter = builder.Eq(sKey1, searchField1);
else
filter = filter & builder.Eq(sKey1, searchField1);
}