Comparison Operators

Overview

Comparison operators are probably the most used operators when querying any type of database provider. MongoDB provides a set of operators πŸ› οΈ that can cover all type of comparisons you might need for your queries. This section presents samples for the following query operators:

Operator

Matches when

Equal

A field's value is equal to a specified value

Not Equal

A field's value is not equal to a specified value

Greater Than

A field's value is greater than a specified value

Greater Than or Equal

A field's value is greater than or equal to a specified value

Less Than

A field's value is less than a specified value

Less Than or Equal

A field's value is less than or equal to a specified value

In

A field's value is contained among specified values

Not In

A field's value is not contained among specified values

Equal operator - $eq

The equal operator is used to match documents having a field value equal to a specific value. You can use it for both top level and embedded documents.

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

The sample uses an equal operator to find all documents that have the profession field (top level field) equal to "Pilot".

ComparisonOperators.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

// Case sensitive!
var equalPilotsFilter = Builders<User>.Filter
    .Eq(u => u.Profession, "Pilot");

var pilots = await collection
    .Find(equalPilotsFilter).ToListAsync();

Not Equal operator - $ne

When you want to match all document that certain field(s) values are not equal to a specific value then you use the Not Equal operator.

Builders<T>.Filter.Ne(doc => doc.<field>, <value>)

The sample uses a not equal operator to match all documents that their profession field is other than "Doctor".

ComparisonOperators.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

var notEqualDoctorsFilter = Builders<User>.Filter
    .Ne(u => u.Profession, "Doctor");
    
var notDoctors = await collection
    .Find(notEqualDoctorsFilter).ToListAsync();

Greater Than operator - $gt

The greater than operator is used to find all documents that the field value is greater than a specific value.

Builders<T>.Filter.Gt(doc => doc.<field>, <value>)

The sample finds all user documents having their salary field greater than 3500.

ComparisonOperators.cs
var collection = database.GetCollection<User>(Constants.UsersCollection);

var filterGreaterThan = Builders<User>.Filter
    .Gt(u => u.Salary, 3500);

var greaterThan3500 = await collection
    .Find(filterGreaterThan).ToListAsync();

Greater Than or Equal operator - $gte

The greater than or equal operator is used to find all documents that the field value is greater than or equal a specific value.

Builders<T>.Filter.Gte(doc => doc.<field>, <value>)

The sample finds all user documents having their salary field greater than or equal to 4500.

ComparisonOperators.cs
var collection = database
  .GetCollection<User>(Constants.UsersCollection);

// create a greater than or equal filter on salary
var filterGreaterOrEqualThan = Builders<User>.Filter
            .Gte(u => u.Salary, 4500);
            
var greaterOrEqualThan4500 = await collection
            .Find(filterGreaterOrEqualThan).ToListAsync();

Less Than operator - $lt

The less than operator is used to find all documents that the field value is less than a specific value.

Builders<T>.Filter.Lt(doc => doc.<field>, <value>)

The sample finds all user documents having their salary field less than 2500.

ComparisonOperators.cs
var collection = database
    .GetCollection<User>(Constants.UsersCollection);

var filterLessThan = Builders<User>.Filter
    .Lt(u => u.Salary, 2500);

var lessThan2500 = await collection
    .Find(filterLessThan).ToListAsync();

Less Than or Equal operator - $lte

The less than or equal operator is used to find all documents that the field value is less than or equal to a specific value.

Builders<T>.Filter.Lte(doc => doc.<field>, <value>)

The sample finds all user documents having their salary field less than or equal to 1500.

ComparisonOperators.cs
var collection = database
  .GetCollection<User>(Constants.UsersCollection);

// create a less than or equal filter on salary
var filterLessOrEqualThan = Builders<User>.Filter
            .Lte(u => u.Salary, 1500);
            
var lessThanOrEqual1500 = await collection
            .Find(filterLessOrEqualThan).ToListAsync();

In operator - $in

The In operator finds documents having a field value contained in a specified array of values.

Builders<T>.Filter.In(doc => doc.<field>, 
    [<value1>,<value2>,..<valueN>])

The sample finds all user documents where their profession field value is either "Dentist", "Pharmacist", or "Nurse" πŸ₯ . This means that all documents matched will have profession value one of the above.

ComparisonOperators.cs
var collection = database
  .GetCollection<User>(Constants.UsersCollection);

// create an In operator filter on profession
var medicalProfessionsFilter = Builders<User>.Filter
  .In(u => u.Profession, new[] { "Dentist", "Pharmacist", "Nurse" });
                
var medicalUsers = await collection
                .Find(medicalProfessionsFilter).ToListAsync();

Not In operator - $nin

The Not In operator finds documents having a field value not contained in a specified array of values.

Builders<T>.Filter.Nin(doc => doc.<field>, 
    [<value1>,<value2>,..<valueN>])

The sample finds all user documents where their profession field value is different than "Dentist", "Pharmacist", or "Nurse" πŸ₯ . This means that all documents matched will not have profession value one of the above.

ComparisonOperators.cs
var collection = database.GetCollection<User>(Constants.UsersCollection);

// create an Not In operator filter on profession
var nonMedicalProfessionsFilter = Builders<User>.Filter
  .Nin(u => u.Profession, new[] { "Dentist", "Pharmacist", "Nurse" });

var nonMedicalUsers = await collection
  .Find(nonMedicalProfessionsFilter).ToListAsync();

Last updated