Basics

Meet the builder 🀝

Filter Definition Builder

To create a filter query for an IMongoCollection<T> collection, you need to build a FilterDefinition<T> filter. You can do this using the Builders<T>.Filter filter definition builder which contains several filters such as the equality or element filters. This section will give you an overview on how to create and use the filter definition builder.

Builders<T>.Filter.<operator>

The Empty filter

Use the Empty filter when you want to get either the 1st document or all of its documents.

Crud.Read.Basics.cs
// empty filter
var emptyFilter = Builders<User>.Filter.Empty;

// first user
var firstUser = await collection.Find(emptyFilter)
    .FirstOrDefaultAsync();

// all users
var allUsers = await collection.Find(emptyFilter)
    .ToListAsync();

When querying a IMongoCollection<BsonDocument> collection, you can use an empty BsonDocument as an empty filter definition

Equality filter on a top level field

The equality filter is one of the most used filters you are gonna use when querying MongoDB. The following examples filter user documents on top level string fields, profession and email.

Builders<T>.Filter.Eq(doc => doc.<field>, <value>)
Crud.Read.Basics.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create the equality filter
var doctorsFilter = Builders<User>.Filter
    .Eq(u => u.Profession, "Doctor");
    
var doctors = await collection.Find(doctorsFilter).ToListAsync();

Equality filter is case sensitive, so always make sure to use it properly!

Equality filter on a nested field

You can use the equality filter to match your documents based on an embedded document field. In the following example the address field is an embedded field on the user document and contains a city string field. The sample show how to filter documents based on the city field.

Builders<T>.Filter
.Eq(doc => doc.<field1>..<fieldN>.<embedded-field>, 
    <value>)

The hierarchy from <field1> to the <embedded-field> cannot contain an array field

Crud.Read.Basics.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create the filter on the address.city field
var athensCityFilter = Builders<User>
    .Filter.Eq(u => u.Address.City, "Athens");
    
var athensUsers = await collection
    .Find(athensCityFilter).ToListAsync();

Remember, the filter might be on an embedded document field, but the result will be always the entire document(s) that matched the criteria

Equality filter on a array field

Assuming your document contains an array field with string values, you want to get all documents that their array field contains a specific value. The sample that follows works for other types as well (e.g. bool, int, decimal, float).

Builders<T>.Filter
    .Eq(doc => doc.<array-field>, <value>)

The example retrieves all user documents that their FavoriteSports array field contains a specific sport. It does that by using an equality filter for an array field called AnyEq.

Crud.Read.Basics.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create an equality filter on an array field
// find all user documents that contains 'Basketball' on their sports array
var basketballFilter = Builders<User>.Filter
    .AnyEq(u => u.FavoriteSports, "Basketball");

// the matched documents might have other sports
// in the favoriteSports array
var usersHaveBasketball = await collection
    .Find(basketballFilter).ToListAsync();

When the search term used in AnyEq is a simple value, then you are running a search for that term in the array field, meaning that there might be other values contained as well

Equality filter on a array field - Exact match

In case you want to run an exact match on the array field then you must use an array argument in the AnyEq filter. This will try match the entire array field rather than just searching inside the array.

Builders<T>.Filter
    .Eq(doc => doc.<array-field>, <array-values>)

The following example finds the documents that their FavoriteSports array field contains only the Soccer term.

Crud.Read.Basics.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// create an equality filter on an array field
// find all user documents that have only 'Soccer' on their sports array
var onlySoccerFilter = Builders<User>.Filter
    .Eq(u => u.FavoriteSports, new List<string> { "Soccer" });

// the matched documents contain only Soccer in the favoriteSports
var soccerUsers = await collection
    .Find(onlySoccerFilter).ToListAsync();

Last updated