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

Comparison Operators

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.

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

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.

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

Greater Than operator - $gt

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

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

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.

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

Less Than operator - $lt

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

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

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.

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

In operator - $in

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

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.

Not In operator - $nin

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

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.

Last updated