MongoDB Operators
Overview
One of the most well-liked NoSQL database systems is MongoDB. This article will give us a wide range of MongoDB operator choices. MongoDB Operators are symbols, just like operators in other programming languages, that instruct the compiler or interpreter to carry out particular logical operations, relational or mathematical and create the desired outcome.
MongoDB Query Operators
To compare and refer to document attributes, you can use a variety of query MongoDB Operators.
The comparison, logical, element, evaluation, Geospatial, array, bitwise, and remark operators are all available as "query operators" in MongoDB. We are going to discuss all these MongoDB Operators one by one in detail with examples.
Comparison Query Operators in MongoDB
When comparing values in queries, the following MongoDB Operators can be used:
-
$eq:
The $eq defines the equality condition (not mandatory). This operator is used to match field values and can make the query more readable and clear to the user.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price of 500.
-
$gt:
To match field values that are greater than (>) a given number, we use the $gt operator.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price greater than 500.
-
$gte:
The $gte selects documents whose field value is larger than or equal to a given value.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price greater than or equal to 500.
-
$in:
The $in operator selects documents where a field's value matches any value in a given array of values.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price in the range of 200 to 500.
-
$It:
The $lt operator selects documents whose field value is less than the given value.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price less than 500.
-
$lte:
The $lt operator selects documents whose field value is less than or equal to the given value.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price less than or equal to 500.
-
$ne:
The $ne operator selects documents whose field value does not match the specified value.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price not equal to 500.
-
$nin:
The $nin operator selects documents with field values that are not in the given array or do not exist.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price not equal to in the given array.
Logical Query Operators in MongoDB
To filter documents from the database based on the reasoning between variables or values, we use MongoDB Logical Query MongoDB Operators. MongoDB provides four different kinds of Logical Operators.
-
$an:
On an array, it performs a logical AND action. The array must contain 1, 2, or more phrases, and it selects the ones that meet all of the phrases in the collection(Array).Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that has a price not equal to 100 and exists.
-
$no:
It acts as a logical NOT on the given phrase, selecting the ones that are unrelated to it.Syntax:
Example:
Suppose we have a collection of books, and we need to find the filter for all the books document that does not have a price greater than 500.
-
$nor:
It acts as a logical NOR on a collection of 1, 2, or more query phrases, and selects the one that fails all of the query phrases in the collection of phrases.Syntax:
Example:
-
$or:
It operates as a logical OR action on an ordering of 1, 2, or more phrases, selecting documents that satisfy any one or more of the phrases.Syntax:
Example:
Evaluation Query Operators in MongoDB
All the Evaluation Query MongoDB Operators are listed below:
-
$expr:
The expr operator enables aggregation expressions to be used within the query language.Syntax:
Example:
-
$jsonSchema:
It matches documents that conform to the given JSON Schema.Syntax:
Example:
-
$mod:
It chooses the document with the specified remainder when data of some field is divided by some divisor.Syntax:
Example:
-
$regex:
It supports regular expressions for pattern matching in query texts. MongoDB makes use of Perl-compatible regular expressions.Syntax:
Example:
-
$text:
The $text operator performs a text search on the field's content, which is stored with a text index.Syntax:
Example:
-
$where:
It is used to send to the query system either a string that has a JS phrase or a complete JS function.Syntax:
Example:
Elements Query Operators in MongoDB
All the Elements Query MongoDB Operators are listed below:
-
$exists:
When the Boolean is true, the exists operator matches the papers that contain the field. It also matches documents with empty field values.Syntax:
Example:
-
$type:
The type operator selects documents whose field value is an instance of the given BSON type.Syntax:
Example:
Geospatial Query Operators in MongoDB
All the Geospatial Query MongoDB Operators are listed below:
-
$geoIntersects:
It only picks those, when the GeoJSON object intersects for those documents' geospatial data.Syntax:
Example:
-
$geoWithin:
The geoWithin operator selects the document containing geospatial data that is completely contained within a specified shape.Syntax:
Example:
The example below selects all loc data that is completely contained within a GeoJSON Polygon. The polygon has a smaller area than a solitary hemisphere:
-
$near:
The near operator specifies a location for which a geospatial query yields documents ranging from close to far.Syntax:
Example:
The following example returns documents that are at least 1000 meters and no more than 5000 meters from the given GeoJSON point, in descending order of proximity:
-
$nearSphere:
It provides the result for the specified location in decreasing order of distance from closest to outermost when the nearsphere operator is used.Syntax:
Example:
The following example gives locations that are at least 1000 meters and no more than 5000 meters from the given point, ordered from closest to farthest:
Array Query Operators in MongoDB
All the Array Query Geospatial Query are listed below:
-
$all:
It selects the document whose field value is an array containing all of the specified components.Syntax:
Example:
-
$elemMatch:
The operator associates documents that have at least one element in an array field that fits all of the query criteria.Syntax:
Example:
-
$size:
It chooses an array containing the number of elements given by the argument. Syntax:Example:
To return all documents in a collection where the field is a two-element array.
Bitwise Query Operators in MongoDB
All the Bitwise Query MongoDB Operators are listed below:
-
$bitsAllClear:
It finds papers where all of the bit positions specified by the query are clear infield.Syntax:
Example:
-
$bitsAllSet:
The bitallset operator finds documents in which all of the bit values specified in the query are set in the field.Syntax:
Example:
-
$bitsAnyClear:
The bitAnyClear operator finds documents where any bit of the query's locations is clear in the field.Syntax:
Example: To return all documents in a collection where the field is a two-element array.
-
$bitsAnySet:
It matches the text where any of the query's bit positions are set in the field.Syntax:
Example:
FAQs
Q. What are the Different Types of Query MongoDB Operators Available?
A. A variety of query MongoDB Operators are supported by MongoDB. The comparison, logical, element, evaluation, Geospatial, array, bitwise,, and remark operators are all available as query operators in MongoDB.
Conclusion
- There is a variety of query MongoDB Operators provided by MongoDB to compare and refer to document attributes.
- To filter documents from the database based on the reasoning between variables or values, we use MongoDB Logical Query operators. It provides four different kinds of Logical MongoDB Operators.
- In MongoDB, we have several comparison query operators that match document values and retrieve data from the MongoDB database. The comparison query operators are listed in the chart below:
- MongoDB Operators are configured to operate with array documents. It provides three different kinds of Array MongoDB Operators.
- Individual fields or the general data structure in a document can be evaluated by MongoDB evaluation query operators. The article section above shows the lists of basic evaluation query MongoDB Operators available.