MongoDB Operators Tutorial – What are Different Operators Available?

Last Updated on

In this MongoDB Operators tutorial, we will be learning different types of operators provided by MongoDB. Basically, we’ve all sorts of operators available in MongoDB as we have in other programming languages. You will be easily able to relate to these operators and understand them quickly.

Before jumping on to the type of operators, let’s quickly revise what operators are. In simple terms, operators are special symbols that tell the compiler or interpreter to carry out specific mathematical or logical manipulations.

Different Types of MongoDB Operators


There are various types of operators provided by MongoDB. Below are the operators, we will be discussing in this article as they help in core MongoDB operations.

1) Comparison Operators

2) Logical Operators

3) Array Operators

4) Element Operators

 

1) MongoDB Comparison Operators


Comparison operators are used to compare two expressions and fetch data (documents) from the MongoDB database (collections) based on their usage in filters.

Various comparison operators in MongoDB are –

  • $eq – Equal To
  • $ne – Not Equal To (!=)
  • $gt – Greater Than (>)
  • $gte – Greater Than Equal To (>=)
  • $lt – Less Than (<)
  • $lte – Less Than Equal To (<=)
  • $in – In (Available in)
  • $nin – Not In (Not available in)
Name Description
$eq Matches values that are equal to the value specified in the query
$ne Matches all values that are not equal to the value specified in the query.
$gt Matches values that are greater than the value specified in the query.
$gte Matches values that are greater than or equal to the value specified in the query.
$lt Matches values that are less than the value specified in the query.
$lte Matches values that are less than or equal to the value specified in the query.
$in Matches any of the values that exist in an array specified in the query.
$nin Matches values that do not exist in an array specified in the query.

Let’s try these Comparison Query Operators.

I am taking a below Collection that I have, to show these operators examples.

MongoDB Operators Collection

As these are very basic operators, I am not demonstrating only a few examples with these operators.

a) $eq (MongDB Equal Operator)

Here, we are fetching documents having MongoDB as Tutorial.

MongoDB Query Operators Equal To

And, don’t get confused. This above query is equivalent to

MongoDB Query Operators Default

b) $ne (MongoDB Not Equal Operator)- Fetch documents not having MongoDB as Tutorial.

MongoDB Comparison Operators NE

c) $in (MongoDB In Operator)- Fetch documents having course duration either 80 days or 90 days.

Note: $in operator uses an array to define the conditions. See below:

MongoDB Query Operators IN

Play around with other comparison operators.

2) MongoDB Logical Operators


These MongoDB operators are used to perform logical operations on the given expressions.  They help to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. Then the value of the conditions is used to determine the overall value of all operators used as a group. Below are various logical operators in MongoDB –

  • $and – AND operator
  • $or – OR operator
  • $nor – NOR operator
  • $not – NOT operator
Name Description
$and Returns all documents that match the conditions of both expressions.
$or Returns all documents that match the conditions of either expression.
$nor Returns all documents that do not match the conditions of either expression
$not Inverts the effect of a query expression and returns documents that do not match the query expression.

Let’s see a few examples of MongoDB Logical Query operators now.

a) $OR (MongoDB OR Operator) – Fetch documents having course duration 80 days or tutorial as Java.

MongoDB Logical Operators OR

b) $nor – Fetch documents not having course duration 80 days or tutorial as Java.

MongoDB Logical Query Operators NOR

c) $not – Fetch documents not having the tutorial as Java.

Note: $not operator needs a regex or a document.

Logical Operators MongoDB NOT

3) MongoDB Array Operators


There are operators specified for projections for the documents having arrays. Below are the 3 operators in Array Query Operators –

Name Description
$all Returns documents from a collection if it matches all values in the specified array.
$size Returns documents from the collection to match an array with the provided number of elements in it.
$elemMatch Returns documents if they Match more than one component (all specified $elemMatch conditions) within an array element.

Let’s try these now. I have created a new collection for this as shown below.

 

a) $size – Find documents where there are 3 elements in testResults array.

size operator Mongo DB

b) $all – Find all the documents having Maths and English both in tags.

All operator in array

c) $elemMatch: Find documents where the subject is Maths and marks are greater than or equal to 95. Below is how you use elemmatch mongodb.

mongodb elemMatch operator

4) MongoDB Element Operators


The next operators in MongoDB operators are element query operators. There are two operators under this category – $exists & $type.

Name Description
$exists Returns documents that have a specific field.
$type Returns documents if field is of a specified type.

Let’s try a few examples.

I am taking different collection here to demonstrate both these operators. Below is the collection.

MongoDB Elements Operators Collection

a) $exists – Find document having “y” element.

Element Operators exists

b) $type – Find documents where “x” is of type “String”.

Note: Here, we are using string (S in lowercase)

type operator in mongodb

That’s all in this article of MongoDB Operators tutorial. There are few more things which could be covered in this but we will cover them later on once relevant concepts are discussed. In the upcoming articles, we will be discussing MongoDB aggregate concepts and MongoDB aggregation operators.

Also Read: MongoDB Projection Tutorial

Don’t forget to share this with your friends. If you have any questions about these MongoDB query operators, please feel free to ask in the comment section. Also, you can share your feedback below.

resume

Follow me

Mohit Arora

Hi, I am Mohit Arora , a passionate blogger and addicted reader. Currently working as a Technology Analyst in INFOSYS Ltd.
You can check my other blog - CatchUpdates.com

You can connect with me on Instagram Or Quora And Linkedin as well.
Mohit Arora
Follow me