Do you think IIT Guwahati certified course can help you in your career?
No
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:
Logical Operators
Comparison Operators
Array Operators
Element Operators
Evaluation Operators
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.
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.
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.
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.
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.
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 operators, Comparison operators, Array operators, Element Operators, Evaluation operators, and Comment operators in detail.