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.

Accumulators Operators

$avg

Computes the average of numeric values for documents in a group, bucket, or window.

$bottom

The $bottom operator returns the last document from the query's result set sorted by one or more fields

$bottomN

The $bottomN operator returns the last N documents from the result sorted by one or more fields

$count

The `$count` operator is used to count the number of documents that match a query filtering criteria.

$first

The $first operator returns the first value in a group according to the group's sorting order.

$firstN

The $firstN operator sorts documents on one or more fields specified by the query and returns the first N document matching the filtering criteria

$last

The $last operator returns the last document from the result sorted by one or more fields

$lastN

The $lastN accumulator operator returns the last N values in a group of documents.

$max

The $max operator returns the maximum value from a set of input values.

$maxN

Retrieves the top N values based on a specified filtering criteria

$median

The $median operator calculates the median value of a numeric field in a group of documents.

$min

Retrieves the minimum value for a specified field

$minN

Retrieves the bottom N values based on a specified filtering criteria

$percentile

The $percentile operator calculates the percentile of numerical values that match a filtering criteria

$stddevpop

The $stddevpop operator calculates the standard deviation of the specified values

$stddevsamp

The $stddevsamp operator calculates the standard deviation of a specified sample of values and not the entire population

$sum

The $sum operator calculates the sum of the values of a field based on a filtering criteria

$top

The $top operator returns the first document from the result set sorted by one or more fields

$topN

The $topN operator returns the first N documents from the result sorted by one or more fields

Aggregation Operators

$addFields

The $addFields stage in the aggregation pipeline is used to add new fields to documents.

$bucket

Groups input documents into buckets based on specified boundaries.

$changeStream

The $changeStream stage opens a change stream cursor to track data changes in real-time.

$collStats

The $collStats stage in the aggregation pipeline is used to return statistics about a collection.

$convert

The $convert operator converts an expression into the specified type

$densify

Adds missing data points in a sequence of values within an array or collection.

$documents

The $documents stage creates a pipeline from a set of provided documents.

$facet

The $facet allows for multiple parallel aggregations to be executed within a single pipeline stage.

$fill

The $fill stage allows filling missing values in documents based on specified methods and criteria.

$geoNear

The $geoNear operator finds and sorts documents by their proximity to a geospatial point, returning distance information for each document.

$group

The $group stage groups documents by specified identifier expressions and applies accumulator expressions.

$indexStats

The $indexStats stage returns usage statistics for each index in the collection.

$isNumber

The $isNumber operator checks if a specified expression is a numerical type

$lookup

The $lookup stage in the Aggregation Framework is used to perform left outer joins with other collections.

$match

The $match stage in the aggregation pipeline is used to filter documents that match a specified condition.

$merge

The $merge stage in an aggregation pipeline writes the results of the aggregation to a specified collection.

$out

The `$out` stage in an aggregation pipeline writes the resulting documents to a specified collection.

$redact

Filters the content of the documents based on access rights.

$replaceWith

The $replaceWith operator in DocumentDB returns a document after replacing a document with the specified document

$sample

The $sample operator in DocumentDB returns a randomly selected number of documents

$set

The $set operator in DocumentDB updates or creates a new field with a specified value

$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.

$sort

The $sort stage in the aggregation pipeline is used to order the documents in the pipeline by a specified field or fields.

$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.

$toBool

The $toBool operator converts an expression into a Boolean type

$toDate

The $toDate operator converts supported types to a proper Date object.

$toDecimal

The $toDecimal operator converts an expression into a Decimal type

$toDouble

The $toDouble operator converts an expression into a Double value

$toInt

The $toInt operator converts an expression into an Integer

$toLong

The $toLong operator converts an expression into a Long value

$toObjectId

The $toObjectId operator converts an expression into an ObjectId

$toString

The $toString operator converts an expression into a String

$unset

The $unset stage in the aggregation pipeline is used to remove specified fields from documents.

$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.

Date Expression Operators

$dateAdd

The $dateAdd operator adds a specified number of time units (day, hour, month etc) to a date.

$dateDiff

The $dateDiff operator calculates the difference between two dates in various units such as years, months, days, etc.

$dateFromParts

The $dateFromParts operator constructs a date from individual components.

$dateFromString

The $dateDiff operator converts a date/time string to a date object.

$dateSubtract

The $dateSubtract operator subtracts a specified amount of time from a date.

$dateToParts

The $dateToParts operator decomposes a date into its individual parts such as year, month, day, and more.

$dateToString

The $dateToString operator converts a date object into a formatted string.

$dateTrunc

The $dateTrunc operator truncates a date to a specified unit.

$dayOfMonth

The $dayOfMonth operator extracts the day of the month from a date.

$dayOfWeek

The $dayOfWeek operator extracts the day of the week from a date.

$dayOfYear

The $dayOfYear operator extracts the day of the year from a date.

$hour

The $hour operator returns the hour portion of a date as a number between 0 and 23.

$isoDayOfWeek

The $isoDayOfWeek operator returns the weekday number in ISO 8601 format, ranging from 1 (Monday) to 7 (Sunday).

$isoWeek

The $isoWeek operator returns the week number of the year in ISO 8601 format, ranging from 1 to 53.

$isoWeekYear

The $isoWeekYear operator returns the year number in ISO 8601 format, which can differ from the calendar year for dates at the beginning or end of the year.

$millisecond

The $millisecond operator extracts the milliseconds portion from a date value.

$minute

The $minute operator extracts the minute portion from a date value.

$month

The $month operator extracts the month portion from a date value.

$second

The $second operator extracts the seconds portion from a date value.

$week

The $week operator returns the week number for a date as a value between 0 and 53.

$year

The $year operator returns the year for a date as a four-digit number.