Comparison Operators
Last updated
Last updated
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
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".
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();
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".
var collection = database
.GetCollection<User>(Constants.UsersCollection);
var notEqualDoctorsFilter = Builders<User>.Filter
.Ne(u => u.Profession, "Doctor");
var notDoctors = await collection
.Find(notEqualDoctorsFilter).ToListAsync();
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.
var collection = database.GetCollection<User>(Constants.UsersCollection);
var filterGreaterThan = Builders<User>.Filter
.Gt(u => u.Salary, 3500);
var greaterThan3500 = await collection
.Find(filterGreaterThan).ToListAsync();
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.
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();
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.
var collection = database
.GetCollection<User>(Constants.UsersCollection);
var filterLessThan = Builders<User>.Filter
.Lt(u => u.Salary, 2500);
var lessThan2500 = await collection
.Find(filterLessThan).ToListAsync();
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.
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();
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>])
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();
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>])
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();
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.
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.