MongoDB Comparison Query Operators Example
Hello readers, in this tutorial, we will understand the different comparison query operators available in the Mongo database. Let’s study in brief the different ways in which these operators can be used.
1. Introduction
If you have installed the MongoDB application on Windows or Ubuntu operating system and you wish to learn the comparison query operators then follow the below steps. It is very simple, but before moving further let’s take a look at MongoDB and its features.
1.1 What is MongoDB?
- MongoDB is a high-performance NoSQL database where each database has collections which in turn has documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (BSN)
- The documents in MongoDB doesn’t need to have a schema defined beforehand. Instead, the fields (i.e. records) can be created on the go
- Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily
- This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability
1.2 Why MongoDB?
- As a NoSQL type database, MongoDB stores the data in the form of a document. Thus, MongoDB offers more flexibility
- This database supports search by field-name, range queries, and the regular expressions. It often provides queries to return the particular fields inside the documents
- MongoDB offers indexes to improve the search performance within the NoSQL database
- To offer horizontal scalability, MongoDB uses sharding by splitting the data across the many MongoDB occurrences
- Replication: MongoDB can give high availability with the replica sets
2. MongoDB Comparison Query Operators Example
In this tutorial, we will learn how to handle the different comparison query operators provided by the Mongo database. But before moving further with the tutorial, we will need to create the sample collection. The following script is used to create a database called warehouse
with a collection as inventory
. Open the Mongo terminal and execute the script.
> use warehouse > db.inventory.insertMany( [ { _id: 101, item: { name: "ab", code: "123" }, qty: 15, tags: [ "A", "B", "C" ] }, { _id: 102, item: { name: "cd", code: "456" }, qty: 20, tags: [ "B" ] }, { _id: 103, item: { name: "ij", code: "789" }, qty: 25, tags: [ "A", "B" ] }, { _id: 104, item: { name: "xy", code: "000" }, qty: 30, tags: [ "B", "A" ] }, { _id: 105, item: { name: "mn", code: "123" }, qty: 20, tags: [ [ "A", "B" ], "C" ] } ] );
If everything goes well, the database and the collections will be shown in the Mongo Workbench.
2.1 $eq, $gt, $gte, $lt, and $lte Operators
In the Mongo database, a comparison operator fetches the required documents from the Mongo database based on the comparison of two expressions. In this section, we are going to briefly discuss the below conditional operators.
- $eq: This operator fetch the documents from a collection that are equal to the given value expression
- $gt: This operator fetch the documents from a collection that are greater than the given value expression
- $gte: This operator fetch the documents from a collection that are greater than or equal to the given value expression
- $lt: This operator fetch the documents from a collection that are less than the given value expression
- $lte: This operator fetch the documents from a collection that are less than or equal to the given value expression
2.1.1 $eq Operator
In the Mongo database, the $eq operator is used to get the documents from a collection where the value of a field equals the given value. Here is what the query syntax will look like.
Syntax
> db.collection_name.find( {<Field_name> : {$eq : [<Value>]}} )
where:
- Field_name is the attribute name on which the document is retrieved
- Value is the given condition which is matched against the attribute value
Let’s understand this with the help of an example.
> db.inventory.find( { qty: { $eq: 20 } } ).pretty()
This command will only get those documents from the inventory
collection where the value of the qty field equals the given value i.e. 20
.
2.1.2 $gt Operator
In the Mongo database, the $gt operator is used to get the documents from a collection where the value of a field is greater than the given value. Here is what the query syntax will look like.
Syntax
> db.collection_name.find( {<Field_name> : {$gt : [<Value>]}} )
where:
- Field_name is the attribute name on which the document is retrieved
- Value is the given condition which is matched against the attribute value
Let’s understand this with the help of an example.
> db.inventory.find( { qty: { $gt: 20 } } ).pretty()
This command will only get those documents from the inventory
collection where the value of the qty field is greater than the given value i.e. 20
.
2.1.3 $gte Operator
In the Mongo database, the $gte operator is used to get the documents where the value of a field is greater than or equal to the given value. Here is what the query syntax will look like.
Syntax
> db.collection_name.find( {<Field_name> : {$gte : [<Value>]}} )
where:
- Field_name is the attribute name on which the document is retrieved
- Value is the given condition which is matched against the attribute value
Let’s understand this with the help of an example.
> db.inventory.find( { qty: { $gte: 20 } } ).pretty()
This command will only get those documents from the inventory
collection where the value of the qty field is greater than or equal to the given value i.e. 20
.
2.1.4 $lt Operator
In the Mongo database, the $lt operator is used to retrieve the documents where the value of a field is less than the specified value. Here is what the query syntax will look like.
Syntax
> db.collection_name.find( {<Field_name> : {$lt : [<Value>]}} )
where:
- Field_name is the attribute name on which the document is retrieved
- Value is the given condition which is matched against the attribute value
Let’s understand this with the help of an example.
> db.inventory.find( { qty: { $lt: 20 } } ).pretty()
This command will only get those documents from the inventory
collection where the value of the qty field is less than the given value i.e. 20
.
2.1.5 $lte Operator
In the Mongo database, the $lte operator is used to retrieve the documents where the value of a field is less than or equal to the specified value. Here is what the query syntax will look like.
Syntax
> db.collection_name.find( {<Field_name> : {$lte : [<Value>]}} )
where:
- Field_name is the attribute name on which the document is retrieved
- Value is the given condition which is matched against the attribute value
Let’s understand this with the help of an example.
> db.inventory.find( { qty: { $lte: 20 } } ).pretty()
This command will only get those documents from the inventory
collection where the value of the qty field is less than or equal to the given value i.e. 20
.
2.2 $in and $nin Operators
The $in and the $nin operators are used to get the documents from a collection where the values in the specified array matches or not with the field value.
2.2.1 $in Operator
In the Mongo database, the $in operator is used to get the documents where the value of a field equals any value in the specified array. Here is what the query syntax will look like.
Syntax
> db.collection_name.find( {<Field_name> : {$in : [<Value 1>, <Value 2>, <Value 3>, ..... <Value N>]}} )
where:
- Field_name is the attribute name on which the document is retrieved
- Value 1 to ‘n’ is the given condition which is matched against the attribute value
Let’s understand this with the help of an example.
> db.inventory.find( { qty: { $in: [ 5, 15 ] } } ).pretty()
This command will only get those documents from the inventory
collection where the value of the qty field value is either 5
or 15
.
2.2.2 $nin Operator
In the Mongo database, the $nin operator get those documents from the Mongo collection where:
- The field value is not in the given array, or
- The field does not exist in the Mongo document
Let’s understand this with the help of an example.
> db.inventory.find( { qty: { $nin: [ 5, 15 ] } } ).pretty()
This command will only get those documents from the inventory
collection where the value of the qty field value does not equal to 5
or 15
. Here, the selected documents can also include those documents that do not contain the qty
field.
2.3 $ne Operator
In the Mongo database, the $ne operator get all documents from a collection except for the specific ones that match the given condition. Here is what the query syntax will look like.
Syntax
> db.collection_name.find( {<Field_name> : {$ne : [<Value>]}} )
where:
- Field_name is the attribute name on which the document is retrieved
- Value is the given condition which is matched against the attribute value
Let’s understand this with the help of an example.
> db.inventory.find( { qty: { $ne: 20 } } ).pretty()
This command will get all documents from the inventory
collection where the value of the qty field value doesn’t match the given value i.e. 20
.
Do note, the pretty()
method used in above commands displays the documents in a structured pattern. If developers want to display the output in a non-structured pattern, they can simply remove this method. That’s all for this post. Happy Learning!!
3. Conclusion
In this tutorial, we learned about the different comparison query operators of the Mongo database. Developers can download the sample commands in the Downloads section.
4. Download the Eclipse Project
This was an example of the different comparison query operators available in the Mongo database.
You can download the full source code of this example here: MongoDBComparisonQueryComparator