Operators in Mongodb
Operators in Mongodb
MongoDB offers different types of operators that can be used to interact with the database.
Operators are special symbols or keywords that inform a compiler or an interpreter to carry out
mathematical or logical operations.
The query operators enhance the functionality of MongoDB by allowing developers to create
complex queries to interact with data sets that match their applications.
Comparison
Logical
Element
Evaluation
Array
Comments
Comparison Operators
MongoDB comparison operators can be used to compare values in a document. The following
table contains the common comparison operators.
Operator Description
$ne Matches values that are not equal to the given value.
Logical Operators
MongoDB logical operators can be used to filter data based on given conditions. These
operators provide a way to combine multiple conditions. Each operator equates the given
condition to a true or false value.
Operator Description
$and Joins two or more queries with a logical AND and returns the documents that match all
the conditions.
$or Join two or more queries with a logical OR and return the documents that match either
query.
$nor The opposite of the OR operator. The logical NOR operator will join two or more queries
and return documents that do not match the given query conditions.
$not Returns the documents that do not match the given query expression.
Element Operators
The element query operators are used to identify documents using the fields of the document.
The table given below lists the current element operators.
Operator Description
$type Matches documents according to the specified field type. These field types are specified
BSON types and can be defined either by type number or alias.
Evaluation Operators
The MongoDB evaluation operators can evaluate the overall data structure or individual field in a
document. We are only looking at the basic functionality of these operators as each of these
operators can be considered an advanced MongoDB functionality. Here is a list of common
evaluation operators in MongoDB.
Operator Description
$mod Matches documents where a given field’s value is equal to the remainder after being
divided by a specified value.
$text Perform a text search on the indicated field. The search can only be performed if the
field is indexed with a text index.
Array Operators
MongoDB array operators are designed to query documents with arrays. Here are the array
operators provided by MongoDB.
Operator Description
$all Matches arrays that contain all the specified values in the query condition.
$size Matches the documents if the array size is equal to the specified size in a query.
$elemMatch Matches documents that match specified $elemMatch conditions within each
array element.
Comment Operator
The MongoDB comment query operator associates a comment to any expression taking a query
predicate. Adding comments to queries enables database administrators to trace and interpret
MongoDB logs using the comments easily.