🌟 Operators Used in MongoDB 🌟

Rahul Kumar
4 min readOct 1, 2022

--

Operators In MongoDB :

1. Comparison Query Operators:

Syntax for comparison query operator :

example :

1. $eq : Matches values that are equal to specified value.

Q. How to retrieve the price of product whose name is iphone 7?

> db.product_catalog.find( 
{ “prodname”: { $eq: “iphone 7” } },
{ _id: 0, price: 1,prodname: 1 }
)

2. $gt: Matches values that are greater than specified field value.

Q. How to retrieve the product names having price greater than ‘60000’:

> db.product_catalog.find(
{ price:{$gt:60000} },
{ _id:0,prodname:1 }
)

Similarly, we can use $gte,$lt and $lte.

3. $ne: Matches all values that are not equal to specified value.

Retrieve the documents that have values other than ‘electronics’ for the main category:

> db.product_catalog.find(
{ "categories.main":{$ne:"electronics"} }
)

4. $lt and $gt:

Retrieve the product names having price greater than equal to ‘60000’ and less than ‘70000’.

> db.product_catalog.find(
{ "price":{$gte:60000,$lt:70000} },
{ _id:0,prodname:1 }
)

In this case, we can also use logical operator ‘and’ and ‘nor’ we will discuss in next section.

2. Logical Query Operators:

Syntax for logical operator :

Example :

Q. Retrieve the product names having price greater than equal to ‘60000’ and less than ‘70000’.

  1. BY ‘and’ operator:
> db.product_catalog.find(
{ $nor: [
{ "price": { $lt: 60000 } },
{ "price": { $gte: 70000 } }
] },
{ _id: 0, prodname: 1 }
)

2. BY ‘nor’ operator:

> db.product_catalog.find(
{ $and: [
{ "price": { $gte: 60000 } },
{ "price": { $lt: 70000 } }
]
},
{ _id: 0, prodname: 1 }
)

Syntax for ‘not’ operator :

example:

Retrieve the products where the ISBN value is not equal to 2343454:

> db.product_catalog.find( 
{ ISBN: { $not: { $eq: 2343454 } }
}
)

3. Array Query Operators:

Before Array Operator, we are going to see some arrays example :

  • ** Order matters in Array.

1. If you want find exact array match with same order then use this :

Q. Retrieve the products where colors field has exactly two elements — ‘white’ and ‘black’ in the order specified:

> db.product_catalog.find( 
{ colors: [“white”, “black”] }
)

there is no exact match in smart phone database.

2. Match specified array elements(without any orders): Using $all operator :

Syntax :

Example:

Q. Retrieve the documents containing both the elements ‘black and ‘gold’ in the colors field, irrespective of the order or presence of other elements:

> db.product_catalog.find(
{ colors : { $all : ["black", "gold"] } }
)
OR
> db.product_catalog.find(
{ colors : { $all : ["gold", "black"] } }
)

3. Array match specifying the number of elements : Using $size operator

In this case, we can use $size operator :

Example :

Q. Retrieve documents that contain exactly 2 elements for the colors field:

> db.product_catalog.find(
{ colors:{ $size:2 } }
)

4. $elematch operator : Match one or more array elements satisfying the criteria.

Syntax :

Q. Retrieve the documents where revisedYears array field contains atleast one element that is both greater than ‘2015’ and less than or equal to ‘2017’:

> db.product_catalog.find( 
{ revisedYears: { $elemMatch: { $gt: 2015, $lte: 2017 } } }
)

we can use $and,$or,$not operator in array too.

If you want to know basics of MongoDB and update operators then you can see this blog of mine.

Thank you….

--

--

Rahul Kumar
Rahul Kumar

Written by Rahul Kumar

MERN Stack Developer building scalable web apps with MongoDB, Express.js, React.js, Node and TS. Sharing insights and best practices for modern web development.

No responses yet