Logical Operators
To be or not to be..
Overview
Logical operators allow you to match documents based on the boolean result β β coming from a set of expressions. The logical expressions are evaluated for each document in your filter and if their boolean result matches the expected one (true or false) are included or excluded respectively in the returned result. The section contains samples for the following operators:
Operator
Description
AND
Matches documents that fulfill all specified conditions
NOT
Matches documents that don't fulfill the specified expression
OR
Matches documents that fulfill at least one of a set of conditions
NOR
Matches documents that fail to fulfill both conditions

AND operator - $and
The AND operator performs a logical AND on a set of expressions and match documents that satisfy all of them.
Create as many filter definitions you want and pass them as an argument to the And FilterDefinitionBuilder method.
The sample uses an And operator to find all documents that have male gender AND have the profession field equal to "Doctor".
The Gender property on the User class is an Enum type and the driver is smart enough π§ββοΈ π¦ to translate it properly when sending the query to MongoDB
You can combine as many filters as you want to an AND operator. The following sample finds all documents that match the following criteria:
They have gender Female π©ββοΈ
They are either "Teacher", Nurse" or "Dentist" π« π₯
Their salary is between 2000 AND 3200 π°
The inner filter definitions passed as arguments in the And method can be as complex as you want to.
NOT operator - $ne
The NOT operator performs a logical NOT on an expression and match documents that don't satisfy the expression.
Create a filter definition and pass it as an argument to the Not FilterDefinitionBuilder method. The boolean expression's result for the matched documents will be false, meaning that they don't satisfy the expression.
The sample uses an Not operator to find all documents having male gender, which can be translated as NOT female.
OR operator - $or
The OR operator performs a logical OR on an set of expressions and matches documents that satisfy at least on of the expressions.
Create as many filter definitions you want and pass them as an argument to the Or FilterDefinitionBuilder method. The filters passed as parameters can be as complex as you want.
The sample uses an Or operator to find all documents having salary, either too low (less than 1500) or too high ( greater than 4000).
NOR operator - $nor
The NOR operator performs a logical NOR on an set of expressions and matches documents that fail to satisfy all the expressions. Despite the fact that MongoDB supports the $nor operator, you won't find any method on the C# driver. That's totally fine though because you can built it using the AND operator and negating the internal filters.
Create as many filter definitions you want to fail and pass them as an argument to the And FilterDefinitionBuilder method. The filters passed as parameters can be as complex as you want.
The sample finds documents that fail to satisfy the following criteria:
Have profession equal to "Doctor"
Have salary less than 4500
Since the matched documents needs to fail the above criteria, you should use their negates with an AND operator.
π‘ Of course you can build a simple extension method that does this for you and makes things a little bit easier.
Last updated