MongoDB Query Language (MQL)

Explore the essential MongoDB Query Language (MQL) operators and commands available in this reference. Each entry includes a brief description and usage details to help you build effective queries and manage your database.

Accumulator Operators

$avg

The `$avg` operator computes the average of numeric values across groups of documents or within defined windows.

$bottom

The `$bottom` operator sorts documents on one or more fields specified by the query and returns the last document matching the filtering criteria.

$bottomN

The `$bottomN` operator sorts documents on one or more fields specified by the query and returns the last N documents matching the filtering criteria.

$count

The `$count` operator is used to count the number of documents that match a specified query filter. The count operator is useful for summarizing data or generating counts for specific groupings.

$first

The `$first` operator sorts documents on one or more fields specified by the query and returns the first document matching the filtering criteria. If no sorting order is specified, the order is undefined.

$firstN

The `$firstN` operator returns the first N values in a group according to the group's sorting order. If no sorting order is specified, the order is undefined.

$last

The `$last` operator sorts documents on one or more fields specified by the query and returns the last document matching the filtering criteria.

$lastN

The `$lastN` accumulator operator returns the last N values in a group of documents for a specified expression. It's useful when you need to retrieve multiple final values from a sorted collection.

$max

The `$max` operator returns the maximum value of a set of input values. It can be used as an accumulator operator in aggregation or as a field update operator to set a field to a value only if the new value is greater than the current value.

$maxN

The `$maxN` operator is used to retrieve the top N values for a field based on a specified filtering criteria. It's useful for identifying the highest values in a dataset.

$median

The `$median` accumulator operator calculates the median value of a numeric field in a group of documents. It's useful for finding the middle value in a distribution of data.

$min

The `$min` operator is used to retrieve the minimum value for a specified field within aggregation stages like `$group`, `$bucket`, `$bucketAuto`, or `$setWindowFields`. It's particularly useful in summarizing data or finding the smallest value in a dataset. When used as a field update operator, it updates a field only if the new value is less than the current value.

$minN

The `$minN` operator is used to retrieve the bottom N values for a field based on a specified filtering criteria. It's useful for identifying the lowest values in a dataset.

$percentile

Calculates the percentile of numerical values that match a filtering criteria, useful for identifying statistical thresholds such as median or percentiles.

$stdDevPop

The `$stdDevPop` operator calculates the standard deviation of the specified values. The operator can only calculate the standard deviation of numeric values.

$stdDevSamp

The `$stdDevSamp` operator calculates the standard deviation by taking a specified sample of the values of a field. The standard deviation is calculated by taking a random sample of the specified size. If a precise standard deviation is needed, $stdDevPop must be used instead.

$sum

The `$sum` operator calculates the sum of numeric values of a field or expression that match a filtering criteria.

$top

The `$top` operator sorts documents on one or more fields specified by the query and returns the first document matching the filtering criteria. It combines sorting and selection in a single operation.

$topN

The `$topN` operator sorts documents on one or more fields specified by the query and returns the first N documents matching the filtering criteria. It extends the functionality of `$top` by allowing you to retrieve multiple top elements.

Aggregation Operators

$addFields

The `$addFields` stage in the aggregation pipeline is used to add new fields to documents. It can also be used to reset the values of existing fields. This stage is particularly useful when you need to create new fields based on existing data or modify existing fields within your documents.

$bucket

The `$bucket` stage in an aggregation pipeline groups input documents into buckets based on specified boundaries. This is especially useful for creating histograms or categorizing data into ranges.

$changeStream

The `$changeStream` stage returns a change stream cursor that tracks changes to a collection. Change streams allow applications to access real-time data changes without the complexity and risk of tailing the oplog.

$collStats

The `$collStats` stage in the aggregation pipeline is used to return statistics about a collection. This stage provides detailed information that can help with database optimization and monitoring.

$convert

The $convert operator converts an expression into a value of the specified type.

$count

The `$count` stage returns a count of the number of documents at this stage of the aggregation pipeline. It outputs a document with a single field containing the count.

$densify

The `$densify` stage in an aggregation pipeline is used to fill in missing data points within a sequence of values. It helps in creating a more complete dataset by generating missing values based on a specified field, range, and step. This is useful in scenarios like time-series data analysis, where gaps in data points need to be filled to ensure accurate analysis.

$documents

The `$documents` aggregation pipeline stage is used to create a pipeline from a set of provided documents. This stage is particularly useful when you want to process specific documents without querying a collection.

$facet

The `$facet` stage allows for multiple parallel aggregations to be executed within a single pipeline stage. It's useful for performing multiple analyses on the same dataset in a single query.

$fill

The `$fill` stage is used to fill missing or null values in documents within the aggregation pipeline. It provides various methods to populate missing data, including using static values, linear interpolation, or values from previous/next documents.

$geoNear

The `$geoNear` aggregation stage calculates distances between a specified point and the location field in each document, sorts the documents by distance, and can optionally limit results by distance.

$group

The `$group` aggregation stage groups documents by specified identifier expressions and applies accumulator expressions to create computed fields for each group. This stage is essential for data aggregation and summarization operations.

$indexStats

The `$indexStats` aggregation stage returns usage statistics for each index in the collection. This stage is useful for analyzing index performance, identifying unused indexes, and optimizing query performance.

$isNumber

The `$isNumber` operator returns true if the input expression is a numerical type. The `$isNumber` operator returns false for an expression of any other type.

$lookup

The `$lookup` stage in the Aggregation Framework is used to perform left outer joins with other collections. It allows you to combine documents from different collections based on a specified condition, enriching documents with related data.

$match

The `$match` stage in the aggregation pipeline is used to filter documents that match a specified condition. It's similar to the `find` operation but is used within the aggregation pipeline to narrow down the documents that pass through to the next stage.

$merge

The `$merge` stage in an aggregation pipeline is used to write the results of the aggregation query into a specified collection. This stage combines data transformation and data persistence in a single operation.

$out

The `$out` stage in an aggregation pipeline allows you to write the resulting documents of the pipeline into a specified collection. It is commonly used to save the output of complex aggregation operations for further use or analysis.

$redact

The `$redact` stage in aggregation pipeline is used to filter fields of the documents in a collection dynamically based on access rights or other conditions. It processes each document and removes or retains fields based on the specified logic.

$replaceWith

The `$replaceWith` aggregation stage operator is used to replace the input document with the specified document. It transforms documents from one structure to another or replaces them entirely with new fields and values.

$sample

The `$sample` stage is used in aggregation pipelines to randomly select a specified number of documents from a collection. The `$sample` command is useful during testing, data analysis, and generating random subsets of data for machine learning.

$set

The `$set` operator updates an existing field or creates a new field with the specified value if it does not exist. One or more fields listed are updated or created. The dot notation is used to update or create nested objects.

$skip

The `$skip` stage in the aggregation pipeline is used to skip a specified number of documents from the input and pass the remaining documents to the next stage in the pipeline. This stage is useful for implementing pagination in queries.

$sort

The `$sort` stage in the aggregation pipeline is used to order the documents in the pipeline by a specified field or fields. This stage helps you sort data, like arranging sales by amount or events by date.

$sortByCount

The $sortByCount stage in the aggregation pipeline is used to group documents by a specified expression and then sort the count of documents in each group in descending order. The `$sortByCount` stage is useful for quickly identifying the most common values within a dataset.

$toBool

The `$toBool` operator converts an expression into a Boolean value. Boolean values are returned as is without a conversion. Nonzero numeric values are converted to true while Decimal, Long, Double or Int values of 0 are converted to false. All other data types are converted to true.

$toDate

The `$toDate` operator converts a specified value into a date type.

$toDecimal

The `$toDecimal` operator converts an input expression into a Decimal value. Long, Double or Int values are simply converted to a Decimal data type, while Decimal values are returned as is. A boolean value of true is converted to 1, while a boolean false is converted to 0. Lastly, ISODates are converted to a Decimal value corresponding to the number of milliseconds since January 1st, 1970 represented by the ISODate value.

$toDouble

The `$toDouble` operator converts a specified value into a Double value.

$toInt

The `$toInt` operator converts a specified value into an integer value.

$toLong

The `$toLong` operator converts a specified value into a Long value.

$toObjectId

The `$toObjectId` operator converts a specified value into an ObjectId.

$toString

The `$toString` operator simply returns the value of the specified expression as a String.

$unset

The $unset stage in the aggregation pipeline is used to remove specified fields from documents. This can be particularly useful when you need to exclude certain fields from the results of an aggregation query for reasons such as privacy, reducing payload size, or simply cleaning up the output.

$unwind

The `$unwind` stage in the aggregation framework is used to deconstruct an array field from the input documents to output a document for each element. This is particularly useful for normalizing data stored in arrays and for performing operations on each element separately.

Arithmetic Expression Operators

$abs

The `$abs` operator returns the absolute value of a number. It removes any negative sign from a number, making it positive.

$add

The `$add` operator adds numbers together or adds numbers and dates. When adding numbers and dates, the numbers are interpreted as milliseconds.

$ceil

The `$ceil` operator computes the ceiling of the input number. This operator returns the smallest integer value that is greater than or equal to the input.

$divide

The `$divide` operator divides two numbers and returns the quotient. The $divide operator returns an error if the divisor is zero.

$exp

The `$exp` operator returns the value of e raised to the specified exponent. The mathematical constant e is approximately equal to 2.71828.

$floor

The `$floor` operator returns the largest integer less than or equal to the specified number.

$ln

The `$ln` operator calculates the natural logarithm (base e) of the input number.

$log

The `$log` operator calculates the logarithm of a number in the specified base.

$log10

The `$log10` operator calculates the logarithm of a number in base 10 and returns the result.

$mod

The `$mod` operator returns the remainder of the division of the first number by the second.

$multiply

The `$multiply` operator calculates the product of the specified input numerical values.

$pow

The `$pow` operator calculates the value of a number raised to a specified exponent.

$round

The `$round` operator is used to round a number to a specified decimal place. It's useful in aggregations where numerical precision is important, such as financial calculations or statistical analysis.

$sqrt

The `$sqrt` operator is used to calculate the square root of a specified number.

$subtract

The `$subtract` operator is used to subtract two numbers and return the result.

$trunc

The `$trunc` operator truncates a number to a specified decimal place.

Array Expression Operators

$arrayElemAt

The `$arrayElemAt` operator is used to return the element at the specified array index. This operator is helpful when you need to extract a specific element from an array within your documents.

$arrayToObject

The `$arrayToObject` operator converts an array into a single document object. This operator is useful when you need to transform arrays into key-value pairs.

$concatArrays

The `$concatArrays` operator is used to combine multiple arrays into a single array. This operator is useful when you need to merge arrays from different documents or fields in a document.

$filter

The `$filter` operator is used to filter elements from an array based on a specified condition. This operator is useful when you need to manipulate or retrieve specific array elements within documents.

$in

The `$in` operator returns a boolean indicating whether a specified value is in an array.

$indexOfArray

The `$indexOfArray` operator searches an array for an occurrence of a specified value and returns the array index of the first occurrence. If the value is not found, returns -1.

$isArray

The `$isArray` operator determines if the operand is an array. Returns a boolean.

$map

The `$map` operator applies an expression to each item in an array and returns an array with the applied results.

$objectToArray

The `$objectToArray` operator converts a document to an array of documents representing key-value pairs.

$range

The `$range` operator outputs an array containing a sequence of integers according to user-defined inputs.

$reduce

The `$reduce` operator applies an expression to each element in an array and combines them into a single value.

$reverseArray

The `$reverseArray` operator accepts an array expression and returns an array with the elements in reverse order.

$slice

The `$slice` operator returns a subset of an array.

$sortArray

The `$sortArray` operator sorts an array based on its elements or a specified sort order.

$zip

The `$zip` operator transposes an array of input arrays so that the first element of the output array contains an array of the first elements of the input arrays, the second element contains an array of the second elements, and so on.

Array Update Operators

$addToSet

The `$addToSet` operator adds a value to an array only if the value is not already present in the array.

$each

The `$each` modifier is used with $push and $addToSet operators to add multiple values to an array field.

$[<identifier>]

The `$[<identifier>]` array update operator is used to update specific elements in an array that match a given condition. This operator is useful when you need to update multiple elements within an array based on certain criteria. It allows for more granular updates within documents, making it a powerful tool for managing complex data structures.

$[]

The `$[]` all positional operator updates all elements in an array for the documents that match the query condition.

$pop

The `$pop` operator is used to remove the first or last element of an array. This operator is useful when you need to manage arrays by removing elements from either end. The `$pop` operator can be used in update operations.

$position

The `$position` operator is used to specify the position in the array where a new element should be inserted. This operator is useful when you need to insert an element at a specific index in an array rather than appending it to the end.

$pull

The `$pull` operator removes from an existing array all instances of a value or values that match a specified condition.

$pullAll

The `$pullAll` operator removes all instances of the specified values from an existing array.

$push

The `$push` operator is used to add a specified value to an array within a document. The $push operator adds new elements to an existing array without affecting other elements in the array.

$slice

The `$slice` modifier limits the number of array elements during a $push operation.

$sort

The `$sort` modifier orders the elements of an array during a $push operation.

$

The `$` positional operator identifies an element in an array to update without explicitly specifying the position of the element in the array. The `$` operator acts as a placeholder for the first element that matches the query condition, and the array field must appear as part of the query document.

Date Expression Operators

$dateAdd

Adds a specified number of time units to a date. It's useful in scenarios where you need to calculate future dates based on a given date and a time interval.

$dateDiff

Calculates the difference between two dates in various units such as years, months, days, etc. It's useful for determining the duration between two timestamps in your dataset.

$dateFromParts

Constructs a date from individual components such as year, month, day, hour, minute, second, and millisecond. This operator can be useful when dealing with data that stores date components separately.

$dateFromString

Used to convert a date/time string to a date object. This operation can be useful when dealing with string representations of dates that need to be manipulated or queried as date objects.

$dateSubtract

Subtracts a specified time unit from a date. It's useful for calculating past dates or intervals in aggregation pipelines.

$dateToParts

Used to extract individual components (Year, Month, Day, Hour, Minute, Second, Millisecond, etc.) from a date object. The operator is useful for scenarios where manipulation or analysis of specific date parts is required, such as sorting, filtering, or aggregating data based on individual date components.

$dateToString

Used to convert a date object to a string in a specified format. It's commonly used in aggregation pipelines to format date fields for reporting, querying, or display purposes. This operator is highly versatile and allows you to define custom date formats.

$dateTrunc

Expression operator truncates a date to the nearest specified unit (for example, hour, day, month). It's useful when working with time-series data or when grouping data by specific time intervals. This operator can be used to simplify and standardize date calculations.

$dayOfMonth

Extracts the day of the month (1–31) from a date value. It's useful for grouping or filtering documents based on the day of the month.

$dayOfWeek

Extracts the day of the week from a date value, where 1 represents Sunday and 7 represents Saturday. It's useful for grouping or filtering documents based on the day of the week.

$dayOfYear

Extracts the day of the year from a date value, where 1 represents January 1. It's useful for grouping or filtering documents based on the day of the year.

$hour

Returns the hour portion of a date as a number between 0 and 23. The operator accepts a date expression that resolves to a Date, Timestamp, or ObjectId.

$isoDayOfWeek

Returns the weekday number in ISO 8601 format, ranging from 1 (Monday) to 7 (Sunday). The operator accepts a date expression that resolves to a Date, Timestamp, or ObjectId.

$isoWeek

Returns the week number of the year in ISO 8601 format, ranging from 1 to 53. The operator accepts a date expression that resolves to a Date, Timestamp, or ObjectId. In ISO 8601, weeks start on Monday and the first week of the year is the week that contains the first Thursday of the year.

$isoWeekYear

Returns the year number in ISO 8601 format. The ISO week-numbering year can differ from the calendar year for dates at the beginning or end of the year. The ISO week year is the year that contains the Thursday of the week in question.

$millisecond

Extracts the milliseconds portion from a date value, returning a number between 0 and 999. This operator is useful for precise timestamp analysis and filtering operations that require millisecond-level granularity.

$minute

Extracts the minute portion from a date value, returning a number between 0 and 59. This operator is commonly used for time-based analysis and scheduling operations.

$month

Extracts the month portion from a date value, returning a number between 1 and 12, where 1 represents January and 12 represents December. This operator is essential for seasonal analysis and monthly reporting.

$second

Extracts the seconds portion from a date value, returning a number between 0 and 59. This operator is useful for precise timestamp analysis and time-sensitive operations that require second-level granularity.

$week

Returns the week number for a date as a value between 0 and 53. Week 0 begins on January 1, and subsequent weeks begin on Sundays. If the date is null or missing, $week returns null.

$year

Returns the year for a date as a four-digit number (for example, 2024). If the date is null or missing, $year returns null.

Geospatial Operators

$box

Defines a rectangular area for geospatial queries using two coordinate pairs, useful for finding locations within a rectangular geographical boundary.

$center

Specifies a circle using legacy coordinate pairs for $geoWithin queries, defining a circle for geospatial query on a flat, Euclidean plane.

$centerSphere

Specifies a circle using spherical geometry for $geoWithin queries, more accurate for Earth-based calculations that account for spherical shape.

$geoIntersects

Selects documents whose location field intersects with a specified GeoJSON object, useful for finding locations that intersect with specific geographical areas.

$geometry

Specifies a GeoJSON geometry object for geospatial queries, used within other geospatial operators to define shapes and points for spatial calculations.

$geoWithin

Selects documents whose location field falls completely within a specified geometry, supporting various shape operators including $box, $polygon, $center, and $geometry.

$maxDistance

Specifies the maximum distance (in meters) that can exist between two points in a geospatial query, typically used with $near for radius-based location searches.

$minDistance

Specifies the minimum distance (in meters) that must exist between two points in a geospatial query, useful for finding locations outside a certain radius.

$near

Returns documents whose location field is near a specified point, sorted by distance, requiring a 2dsphere index and returning documents from nearest to farthest.

$nearSphere

Returns documents with location fields near a specified point on a sphere, calculating distances using spherical geometry, more accurate for Earth-based calculations than $near.

$polygon

Defines a polygon for geospatial queries, allowing you to find locations within an irregular shape, useful for querying locations within complex geographical boundaries.

Window Operators

$covariancePop

Calculates the population covariance of two numerical expressions within a specified document window, used in statistical analysis to measure how two variables change together.

$covarianceSamp

Calculates the sample covariance of two numerical expressions within a specified document window, providing unbiased covariance estimate for statistical analysis.

$denseRank

Assigns consecutive ranking positions to documents within a partition based on specified sort order, with no gaps in rank values for tied documents.

$derivative

Calculates the average rate of change of a field between the first and last documents within a specified window, useful for trend analysis.

$documentNumber

Assigns sequential document numbers to each document within a partition based on specified sort order, providing positional ranking.

$expMovingAvg

Calculates the exponential moving average of field values, giving higher weight to recent documents in the calculation for trend analysis.

$integral

Calculates the area under a curve based on specified range of documents sorted by a specific field, useful for cumulative analysis.

$linearFill

Interpolates missing values in a sequence of documents using linear interpolation, useful for filling gaps in time-series or ordered data.

$locf

Propagates the last observed non-null value forward within a partition, useful for filling missing data points in time-series datasets.

$rank

Assigns rank to each document within a partition based on specified sort order, with gaps in ranking for tied values.

$shift

Shifts values within a partition and returns the shifted value, useful for comparing values from adjacent documents in sorted partitions.