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.
aggregate
The `aggregate` command is used to process data records and return computed results. It performs operations on the data, such as filtering, grouping, and sorting, and can transform the data in various ways.
count
The `count` command is used to count the number of documents in a collection that match specific criteria.
distinct
The `distinct` command is used to find the unique values for a specified field across a single collection. This command is useful when you need to identify the set of distinct values for a field without retrieving all the documents or when you need to perform operations like filtering or grouping based on unique values.
delete
The `delete` command is used to remove documents from a collection. A single document or multiple documents can be deleted based on a specified query filter.
find
The `find` command is used to query documents within a collection. This command is fundamental for data retrieval operations and can be customized with filters, projections, and query options to fine-tune the results.
findAndModify
The `findAndModify` command is used to atomically modify and return a single document. This command is useful for operations that require reading and updating a document in a single step, ensuring data consistency. Common use cases include implementing counters, queues, and other atomic operations.
getMore
The `getMore` command is used to retrieve extra batches of documents from an existing cursor. This command is useful when dealing with large datasets that can't be fetched in a single query due to size limitations.
insert
The `insert` command is used to create new documents into a collection. Either a single document or multiple documents can be inserted in one go.
update
The `update` command is used to modify existing documents within a collection. The `update` command can be used to update one or multiple documents based on filtering criteria. Values of fields can be changed, new fields and values can be added and existing fields can be removed.
$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.
$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.
$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.
$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.
$all
The `$all` operator selects the documents where the value of a field is an array that contains all the specified elements.
$elemMatch
The `$elemMatch` operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
$size
The `$size` operator matches any array with the specified number of elements.
$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.
$bitAnd
The `$bitAnd` operator performs a bitwise AND operation on integer values. It compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
$bitNot
The `$bitNot` operator performs a bitwise NOT operation on integer values. It inverts all the bits of the operand, turning 1s into 0s and 0s into 1s. The result is the bitwise complement of the input value.
$bitOr
The `$bitOr` operator performs a bitwise OR operation on integer values. It compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. If both bits are 0, the corresponding result bit is set to 0.
$bitXor
The `$bitXor` operator performs a bitwise exclusive OR (XOR) operation on integer values. The XOR operation returns 1 for each bit position where the corresponding bits of the operands are different, and 0 where they are the same.
$bitsAllClear
The `$bitsAllClear` operator is used to match documents where all the bit positions specified in a bitmask are clear (that is, 0). This operator is useful in scenarios where you need to filter documents based on specific bits being unset in a binary representation of a field.
$bitsAllSet
The `$bitsAllSet` operator is used to match documents where all the specified bit positions are set (that is, are 1). This operator is useful for performing bitwise operations on fields that store integer values.
$bitsAnyClear
The `$bitsAnyClear` operator is used to match documents where any of the bit positions specified in a bitmask are clear (that is, 0). This operator is useful for filtering documents based on at least one bit being unset.
$bitsAnySet
The `$bitsAnySet` operator is used to match documents where any of the specified bit positions are set (that is, are 1). This operator is useful for filtering documents based on at least one bit being set.
$and
Performs logical AND operation on array of expressions, returning true only if all expressions evaluate to true.
$not
Performs logical NOT operation on an expression, returning the opposite boolean value of the input expression.
$or
Performs logical OR operation on array of expressions, returning true if any expression evaluates to true.
$cmp
The `$cmp` operator compares two specified values. The $cmp operator returns -1 if the first value is less than the second, 0 if the two values are equal and 1 if the first value is greater than the second.
$eq
The `$eq` operator is used to match documents where the value of a field is equal to a specified value.
$gt
The `$gt` operator retrieves documents where the value of a field is greater than a specified value.
$gte
The `$gte` operator retrieves documents where the value of a field is greater than or equal to a specified value.
$in
The `$in` operator matches values of a field against an array of possible values.
$lt
The `$lt` operator retrieves documents where the value of a field is strictly less than a specified value.
$lte
The `$lte` operator retrieves documents where the value of a field is less than or equal to a specified value.
$ne
The `$ne` operator retrieves documents where the value of a field doesn't equal a specified value.
$nin
The `$nin` operator retrieves documents where the value of a specified field doesn't match any value in a list.
$cond
The `$cond` operator is used to evaluate a condition and return one of two expressions based on the result. It's similar to the ternary operator in many programming languages.
$ifNull
The `$ifNull` operator is used to evaluate an expression and return a specified value if the expression resolves to `null`.
$switch
The `$switch` operator is used to evaluate a series of conditions and return a value based on the first condition that evaluates to true.
$binarysize
The `$binarySize` operator is used to return the size of a binary data field. This can be useful when dealing with binary data stored, such as images, files, or any other binary content.
$bsonsize
The `$bsonSize` operator is used to return the size of a document in bytes when encoded as BSON. It's useful for understanding the storage requirements of documents within your collections.
$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.
$expr
Allows the use of aggregation expressions within the query language for complex field comparisons and calculations.
$jsonSchema
Validates documents against a JSON Schema specification to ensure data structure and type conformity.
$mod
Performs a modulo operation and matches documents where the field value modulo divisor equals the specified remainder.
$regex
The `$regex` operator is used to perform pattern matching with regular expressions.
$text
Performs full-text search on string content with support for language-specific stemming, case sensitivity, and phrase matching.
$currentDate
Sets the value of a field to the current date, either as a Date or a timestamp, useful for tracking document modifications.
$inc
Increments the value of a field by a specified amount, creating the field if it doesn't exist, accepting positive and negative values.
$max
Updates a field only if the specified value is greater than the current field value, useful for maintaining maximum values.
$min
Updates a field only if the specified value is less than the current field value, useful for maintaining minimum values.
$mul
Multiplies the value of a field by a specified number, creating the field with value zero if it doesn't exist, useful for scaling and percentage operations.
$rename
Renames fields in documents during update operations, removing the old field name and creating a new field with the same value.
$set
Sets the value of fields in documents, creating new fields if they don't exist or updating existing field values.
$setOnInsert
Sets field values only when an upsert operation results in inserting a new document, having no effect when updating existing documents.
$unset
Removes specified fields from documents, effectively deleting the field and its value entirely.
$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.
$and
The `$and` operator performs a logical AND operation on an array of expressions and retrieves documents that satisfy all the expressions.
$nor
The `$nor` operator performs a logical NOR operation on an array of expressions and selects documents that fail all the specified expressions.
$not
The `$not` operator performs a logical NOT operation on a specified expression and selects documents that don't match the expression.
$or
The `$or` operator performs a logical OR operation on an array of expressions and retrieves documents that satisfy at least one of the specified conditions.
$comment
Adds a comment to a query without affecting the query execution, useful for documentation and debugging purposes.
$getField
Returns the value of a specified field from a document, with support for dynamically determined field names using expressions and variables.
$natural
Controls the order in which documents are returned by specifying natural order (insertion order) or reverse natural order.
$rand
Returns a random floating-point number between 0 and 1, useful for random sampling and generating random values in aggregation pipelines.
$sampleRate
Randomly selects documents from the input at the specified rate, useful for sampling large datasets or creating representative subsets for analysis.
$mergeObjects
Combines multiple documents into a single document, merging fields from the input documents with later documents overriding fields from earlier documents.
$objectToArray
Converts an object to an array of key-value pairs, where each element is a document with 'k' (key) and 'v' (value) fields.
$setField
Adds, updates, or removes fields in a document, allowing dynamic field manipulation with support for computed field names and conditional operations.
$elemMatch
Projects only the first element from an array that matches the specified query condition, useful for returning specific array elements.
$meta
Returns metadata associated with documents in the result set, particularly useful for accessing text search scores and other query execution metadata.
$slice
Controls the number of elements returned from an array field, allowing you to limit array results or skip elements.
$allElementsTrue
Returns true if all elements in an array are true, treating null, undefined, and missing values as false. Empty arrays return true.
$anyElementTrue
Returns true if any element in an array is true, treating null, undefined, and missing values as false. Empty arrays return false.
$setDifference
Returns an array containing elements that exist in the first set but not in the second set, performing set subtraction operation.
$setEquals
Returns true if two arrays contain the same distinct elements, regardless of order or duplicates, performing set equality comparison.
$setIntersection
Returns an array containing elements that exist in all input arrays, performing set intersection operation to find common elements.
$setIsSubset
Returns true if all elements of the first array are contained in the second array, performing subset validation regardless of element order.
$setUnion
Returns an array containing all distinct elements from the input arrays, performing set union operation to combine arrays without duplicates.
$tsIncrement
Extracts the increment portion from a timestamp value, returning the increment component of a MongoDB timestamp which consists of time and increment values.
$tsSecond
Extracts the seconds portion from a timestamp value, returning the time component in seconds since Unix epoch (January 1, 1970, 00:00:00 UTC).
$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.