Table of contents
1.
Introduction 
2.
Different types of MongoDB Operators 
3.
Logical Query Operators
4.
Comparison Query Operators
5.
Array Query Operators
6.
Element Query Operators
7.
Evaluation Query Operators
8.
Comment Query Operators
9.
Frequently Asked Questions
10.
Key Takeaways
Last Updated: Mar 27, 2024

MongoDB operators

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

MongoDB is one of the most popular NoSQL databases. This article will provide us with ample options for MongoDB operators. Just like operators in other programming languages, MongoDB operators are symbols that tell the compiler or interpreter to perform the specific relational, mathematical, or logical operations and produce the final result.

Different types of MongoDB Operators 

There are various types of MongoDB Operators. These operators help us perform the core operations in MongoDB like querying, updating, deleting, etc. This article will discuss the following types of MongoDB Operators:

  1. Logical Operators
  2. Comparison Operators
  3. Array Operators
  4. Element Operators
  5. Evaluation Operators
  6. Comment Operators

Logical Query Operators

We use Mongodb Logical Query operators to filter documents from the database based upon the logic between variables or values. There are four types of Logical Operators provided by MongoDB.

Name Description
$and We use this operator to join query clauses with a logical AND and return the documents matching the given conditions of both clauses.
$or We use the $or operator to join query clauses with a logical OR and return the documents matching the given conditions of either clause.
$nor We use the $nor operator to join query clauses with a logical NOR and return the documents which fail to match both the clauses.
$not We use the $not operator to invert the effect of the query expressions and return documents that do not match the query expression.

 

Syntax:

  • $or - db.<collectionname>.find({$or: [{condition 1}, {condition 2}]})
  • $and - db.<collectionname>.find({$and: [{condition 1}, {condition 2}]})
  • $nor - db.<collectionname>.find({$nor: [{condition 1}, {condition 2}]})
  • $not - db.<collectionname>.find({$not: [{condition 1}, {condition 2}]})

Comparison Query Operators

In MongoDB, we have various comparison query operators that compare the values of the documents and fetch the data from the MongoDB database. The following table contains the comparison query operators:

Name Description
$eq we use this operator to match the values of the fields that are equal(==) to a specified value.
$ne We use the $ne operator to match all values of the field that are not equal (!=) to a specified value.
$gt We use the $gt operator to match values of the fields that are greater than (>) a specified value.
$gte We use the $gte operator to match values of the fields that are greater than equal to (>=) the specified value.
$lt We use the $lt operator to match values of the fields that are lesser than (<) the specified value.
$lte We use the $lte operator to match values of the fields that are lesser than equal to (<=) the specified value.
$in We use the $in operator to match values that exist in the array.
$nin We use the $nin operator to match values that don’t exist in the array.

 

Syntax:

  • $eq - db.<collectionname>.find({ <Property Name> : { $eq : <value>}})
  • $ne - db.<collectionname>.find({ <Property Name> : { $ne : <value>}})
  • $gt - db.<collectionname>.find({ <Property Name> : { $gt : <value>}})
  • $gte - db.<collectionname>.find({ <Property Name> : { $gte : <value>}})
  • $lt - db.<collectionname>.find({ <Property Name> : { $lt : <value>}})
  • $lte - db.<collectionname>.find({ <Property Name> : { $lte : <value>}})
  • $in - db.<collectionname>.find({ <Property Name> : { $in :[<value1>, <value2> ]}})
  • $nin - db.<collectionname>.find({ <Property Name> : { $nin :[<value1>, <value2> ]}})

Array Query Operators

We have MongoDB Operators that are specified to work with the array document. There are three types of Array Operators provided by MongoDB.

Name Description
$all we use the $all operator to find the documents from a collection matching all values in the specified array.
$size We use the $size operator to find the documents from the collection if the array field is of specified size.
$elemMatch We use the $elemMatch operator to select the documents if the element in the array matches all the specified conditions.

 

Syntax:

  • $all - db.<collectionname>.find({ <Array Name> : { $all : [<value1>, <value2> ]}})
  • $size - db.<collectionname>.find({ <Array Name> : { $size : <value>}})
  • $elemMatch - db.<collectionname>.find(<Array Name> : {$elemMatch : {<condition1>, <condition2>}})

Element Query Operators

Element Query Operators are MongoDB Operators that can be used to find documents matching the specified fields or types. There are two types of Element Operators provided by MongoDB.

Name Description
$exists we use the $exists operator to find the documents that have the specified field.
$type We use the $type operator to find the documents that have the specified data types.

 

Syntax:

  • $exists - db.collection_name.find( { <field_name>: { $exists: <boolean_value> } } )
  • $type - db.collection_name.find( { <field_name>: { $type: < BSON_Type> } } )

Evaluation Query Operators

MongoDB evaluation query operators can evaluate individual fields or the overall data structure in a document. The table below shows a list of standard evaluation query operators in MongoDB.

Name Description
$expr The $expr operator allows aggregation expressions within the query language.
$jsonSchema We use the $jsonSchema operator to validate the document against the specified JSON (JavaScript Object Notation) schema.
$mod We use the $mod operator to find the modulo of the given value and select documents with the specified result.
$regex We use the $regex operator to select the documents matching the given regular expression.
$where The $where operator is used for selecting the documents that satisfy a JavaScript expression.
$text $text operator performs text search on the indicated field.

 

Syntax:

  • $expr - db.collection_name.find( { $expr: { <expression> } } )
  • $jsonSchema -  db.collection_name.find( { $jsonSchema: <schema_name> } )
  • $mod - db.collection_name.find( { <Property Name> : { $mod : [<value1>, <value2>] } } )
  • $regex - db.collection_name.find( { <Property Name> : { $regex : /pattern/ } } )
  • $where - db.collection_name.find( { $where: <string|JavaScript Code> } )
  • $text - db.collection_name.find( { $text: { $search: <string> } } )

Comment Query Operators

The comment query operator is associated with a comment in any expression. Adding comments to queries helps database administrators to find and interpret MongoDB logs with the comments easily.

Name Description
$comment The $comment meta-operator makes it possible to attach a comment to a query in any context.

 

Syntax:

  • $comment - db.collection_name.find( { $query: { <query> }, $comment: <comment> } } )

Frequently Asked Questions

1. MongoDB $abs operator?

Ans.  MongoDB $abs operator is used to find the absolute value of the specified number. 

Syntax: { $abs: <number> }.

2. What are some other operators in MongoDB?

Ans. Some other operators in MongoDB are Bitwise Operators, Projection Operators, Field Operators, Geospatial Operators, etc.

3. What are Bitwise operators in MongoDB?

Ans. MongoDB provides a bitwise query operator to perform a bitwise update of any field. This operator supports bitwise xor, bitwise or, and bitwise and operations. For example, { $bit: { <field1>: { <or|and|xor>: <int> } } }.

Key Takeaways

In this article, we learned various types of MongoDB operators available. We discussed Logical operatorsComparison operatorsArray operatorsElement Operators, Evaluation operators, and Comment operators in detail.

Read about Bitwise Operators in C here.

If you want to learn advanced web development, Coding Ninjas has one of the best courses available, which you can find here.

Live masterclass