Javatpoint Logo
Javatpoint Logo

MongoDB Query and Projection Operator

The MongoDB query operator includes comparison, logical, element, evaluation, Geospatial, array, bitwise, and comment operators.

MongoDB Comparison Operators

$eq

The $eq specifies the equality condition. It matches documents where the value of a field equals the specified value.

Syntax:

Example:

The above example queries the books collection to select all documents where the value of the price filed equals 300.

$gt

The $gt chooses a document where the value of the field is greater than the specified value.

Syntax:

Example:

$gte

The $gte choose the documents where the field value is greater than or equal to a specified value.

Syntax:

Example:

$in

The $in operator choose the documents where the value of a field equals any value in the specified array.

Syntax:

Example:

$lt

The $lt operator chooses the documents where the value of the field is less than the specified value.

Syntax:

Example:

$lte

The $lte operator chooses the documents where the field value is less than or equal to a specified value.

Syntax:

Example:

$ne

The $ne operator chooses the documents where the field value is not equal to the specified value.

Syntax:

Example:

$nin

The $nin operator chooses the documents where the field value is not in the specified array or does not exist.

Syntax:

Example:

MongoDB Logical Operator

$and

The $and operator works as a logical AND operation on an array. The array should be of one or more expressions and chooses the documents that satisfy all the expressions in the array.

Syntax:

Example:

$not

The $not operator works as a logical NOT on the specified expression and chooses the documents that are not related to the expression.

Syntax:

Example:

$nor

The $nor operator works as logical NOR on an array of one or more query expression and chooses the documents that fail all the query expression in the array.

Syntax:

Example:

$or

It works as a logical OR operation on an array of two or more expressions and chooses documents that meet the expectation at least one of the expressions.

Syntax:

Example:

MongoDB Element Operator

$exists

The exists operator matches the documents that contain the field when Boolean is true. It also matches the document where the field value is null.

Syntax:

Example:

$type

The type operator chooses documents where the value of the field is an instance of the specified BSON type.

Syntax:

Example:

MongoDB Evaluation Operator

$expr

The expr operator allows the use of aggregation expressions within the query language.

Syntax:

Example:

$jsonSchema

It matches the documents that satisfy the specified JSON Schema.

Syntax:

$mod

The mod operator selects the document where the value of a field is divided by a divisor has the specified remainder.

Syntax:

Example:

$regex

It provides regular expression abilities for pattern matching strings in queries. The MongoDB uses regular expressions that are compatible with Perl.

Syntax:

Example:

$text

The $text operator searches a text on the content of the field, indexed with a text index.

Syntax:

Example:

$where

The "where" operator is used for passing either a string containing a JavaScript expression or a full JavaScript function to the query system.

Example:

MongoDB Geospatial Operator

$geoIntersects

It selects only those documents whose geospatial data intersects with the given GeoJSON object.

Syntax:

Example:

$geoWithin

The geoWithin operator chooses the document with geospatial data that exists entirely within a specified shape.

Syntax:

$near

The near operator defines a point for which a geospatial query returns the documents from close to far.

Syntax:

Example:

$nearSphere

The nearsphere operator specifies a point for which the geospatial query returns the document from nearest to farthest.

Syntax:

Example:

$all

It chooses the document where the value of a field is an array that contains all the specified elements.

Syntax:

Example:

$elemMatch

The operator relates documents that contain an array field with at least one element that matches with all the given query criteria.

Syntax:

Example:

$size

It selects any array with the number of the element specified by the argument.

Syntax:

MongoDB Bitwise Operator

$bitsAllClear

It matches the documents where all the bit positions given by the query are clear infield.

Syntax:

Example:

$bitsAllSet

The bitallset operator matches the documents where all the bit positions given by the query are set in the field.

Syntax:

Example:

$bitsAnyClear

The bitAnyClear operator matches the document where any bit of positions given by the query is clear in the field.

Syntax:

Example:

$bitsAnySet

It matches the document where any of the bit positions given by the query are set in the field.

Syntax:

Example:

MongoDB comments operator

$comment

The $comment operator associates a comment to any expression taking a query predicate.

Syntax:

Example:

MongoDB Projection Operator

$

The $ operator limits the contents of an array from the query results to contain only the first element matching the query document.

Syntax:

$elemMatch

The content of the array field made limited using this operator from the query result to contain only the first element matching the element $elemMatch condition.

Syntax:

$meta

The meta operator returns the result for each matching document where the metadata associated with the query.

Syntax:

Example:

$slice

It controls the number of values in an array that a query returns.

Syntax:

Example:






Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA