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.
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.
>db.demo.find({"tutorial":{$eq:"MongoDB"}}).pretty(); //You can ignore pretty. It is just to show output in a formatted way.
And, don’t get confused. This above query is equivalent to
> db.demo.find({"tutorial":"MongoDB"}).pretty();
b) $ne (MongoDB Not Equal Operator)- Fetch documents not having MongoDB as Tutorial.
> db.demo.find({"tutorial":{$ne:"MongoDB"}}).pretty();
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:
> db.demo.find({"Course Duration":{$in:["80 days","90 days"]}}).pretty();
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.
> db.demo.find({$or:[{"Course Duration":"80 days"},{"tutorial":"Java"}]}).pretty();
b) $nor – Fetch documents not having course duration 80 days or tutorial as Java.
> db.demo.find({$nor:[{"Course Duration":"80 days"},{"tutorial":"Java"}]}).pretty();
c) $not – Fetch documents not having the tutorial as Java.
Note: $not operator needs a regex or a document.
> db.demo.find({"tutorial":{$not:{$eq:"Java"}}}).pretty();
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.
> db.Demo2.find().pretty(); { "_id" : ObjectId("5d9797c791ded6f788c43d16"), "testResults" : [ { "subject" : "Maths", "marks" : 95 }, { "subject" : "English", "marks" : 80 } ], "tags" : [ "Maths", "English" ] } { "_id" : ObjectId("5d9797d291ded6f788c43d17"), "testResults" : [ { "subject" : "Maths", "marks" : 90 }, { "subject" : "Science", "marks" : 80 } ], "tags" : [ "Maths", "Science" ] } { "_id" : ObjectId("5d9797dc91ded6f788c43d18"), "testResults" : [ { "subject" : "Maths", "marks" : 95 }, { "subject" : "Science", "marks" : 80 }, { "subject" : "English", "marks" : 80 } ], "tags" : [ "Maths", "Science", "English" ] } >
a) $size – Find documents where there are 3 elements in testResults array.
> db.Demo2.find({"testResults":{$size:3}}).pretty();
b) $all – Find all the documents having Maths and English both in tags.
> db.Demo2.find({"tags":{$all:["Maths","English"]}}).pretty();
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.
> db.Demo2.find( { "testResults": { $elemMatch: { "subject" : "Maths","marks":{$gte:95}}}}).pretty();
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.
a) $exists – Find document having “y” element.
> db.Demo1.find({"y":{$exists:true}}).pretty();
b) $type – Find documents where “x” is of type “String”.
> db.Demo1.find({"x":{$type:"string"}}).pretty();
Note: Here, we are using string (S in lowercase)
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.
- MongoDB Operators Tutorial – What are Different Operators Available? - October 5, 2019
- MongoDB Projection Tutorial : Return Specific Fields From Query - March 9, 2019
- MongoDB Index Tutorial – Create Index & MongoDB Index Types - July 6, 2018
Very Interesting Tutorials.Thanks Mohit Sir