Filters in Mongo C# — Digitteck
Filters in Mongo C#
dotnet·7 May 2019·4 min read

Filters in Mongo C#

The MongoDB C# driver exposes several ways to write filters for querying collections. Each approach has the same underlying behaviour — the driver converts them into valid MongoDB query syntax — but they differ in verbosity, type safety, and composability.

Model and BSON Attributes

[BsonElement] decouples the C# property name from the document field name. [BsonId] maps the _id field, and [BsonRepresentation] controls how the ID is stored (ObjectId vs string):

csharp
// Book entity with BSON attributes
public class Book
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId Id { get; set; }

    [BsonElement("Name")]
    public string Name { get; set; }

    [BsonElement("Author")]
    public string Author { get; set; }

    [BsonElement("InStock")]
    public bool InStock { get; set; }
}

Connecting and Seeding

csharp
// Connect to MongoDB and get the collection
MongoServerAddress server   = new MongoServerAddress("127.0.0.1", 27017);
MongoClientSettings settings = new MongoClientSettings { Server = server };
MongoClient client           = new MongoClient(settings);

IMongoDatabase database           = client.GetDatabase("FreeBooksLibrary");
IMongoCollection<Book> collection = database.GetCollection<Book>("Books");

// Seed the collection
collection.InsertMany(new List<Book>
{
    new Book { Author = "Justine Picardie", Name = "Inge Morath: On Style",  InStock = true  },
    new Book { Author = "Justine Picardie", Name = "If the Spirit Moves You", InStock = false },
    new Book { Author = "Justine Picardie", Name = "Wish I May",              InStock = false },
    new Book { Author = "Tembi Locke",       Name = "From Scratch",            InStock = true  },
});

1. LINQ Expression Syntax

The most concise option. The driver translates the lambda into MongoDB query syntax:

csharp
// 1. LINQ expression — the driver translates the lambda into MongoDB query syntax
var books = collection
    .Find(x => x.Author == "Justine Picardie" && x.InStock)
    .ToList();

2. Builders Filter Definition

Creates explicit, reusable FilterDefinition<T> objects. Slightly less processing overhead than LINQ expressions and composable with And, Or:

csharp
// 2. Builders<T>.Filter — create named FilterDefinitions and combine with And
FilterDefinition<Book> nameFilter    = Builders<Book>.Filter.Eq(x => x.Author, "Justine Picardie");
FilterDefinition<Book> inStockFilter = Builders<Book>.Filter.Eq(x => x.InStock, true);
FilterDefinition<Book> combined      = Builders<Book>.Filter.And(nameFilter, inStockFilter);

var books = collection.Find(combined).ToList();

3. ExpressionFieldDefinition + Bitwise &

The driver overloads the & operator on FilterDefinition<T>, so you can combine filters without calling Builders.Filter.And:

csharp
// 3. ExpressionFieldDefinition + bitwise & operator
// FilterDefinition overloads the & operator so you can compose filters without calling .And()
var nameField    = new ExpressionFieldDefinition<Book, string>(x => x.Author);
var inStockField = new ExpressionFieldDefinition<Book, bool>(x => x.InStock);

FilterDefinition<Book> nameFilter    = Builders<Book>.Filter.Eq(nameField, "Justine Picardie");
FilterDefinition<Book> inStockFilter = Builders<Book>.Filter.Eq(inStockField, true);

var books = collection.Find(nameFilter & inStockFilter).ToList();

4. StringFieldDefinition

Targets a field by its BSON element name as a string — useful when targeting fields dynamically or when the C# property name is not available:

csharp
// 4. StringFieldDefinition — target a field by its BSON element name as a string
var nameField    = new StringFieldDefinition<Book, string>("Author");
var inStockField = new StringFieldDefinition<Book, bool>("InStock");

FilterDefinition<Book> nameFilter    = Builders<Book>.Filter.Eq(nameField, "Justine Picardie");
FilterDefinition<Book> inStockFilter = Builders<Book>.Filter.Eq(inStockField, true);

var books = collection.Find(nameFilter & inStockFilter).ToList();

5. IN Operator

Matches documents where a field's value is in a provided list, equivalent to SQL IN:

csharp
// 5. IN operator — match any value in a list (equivalent to SQL IN)
var authorNames = new[] { "Justine Picardie", "Tembi Locke" };
FilterDefinition<Book> inFilter = Builders<Book>.Filter.In(x => x.Author, authorNames);

var books = collection.Find(inFilter).ToList();

Tags

.NETC#MongoDB
digitteck

© 2026 Digitteck