Skip to main content
MongoDB uses a multi-stage pipeline model where documents flow through an ordered sequence of transformation stages. DQL uses a single declarative SELECT statement where filtering, projection, grouping, sorting, and limiting are all expressed at once. There are no stages to compose β€” the query optimizer decides execution order. This means most MongoDB aggregations collapse to a single DQL SELECT. Operations with no DQL equivalent β€” joins, window functions, set algebra, advanced math, and several array accumulators β€” are noted as gaps throughout and collected in the Known Gaps section.

AI Agent Prompt

Use this prompt with any AI coding assistant to convert a MongoDB aggregation pipeline to DQL.

Quick Reference

The table below covers the most common MongoDB aggregation pipeline stages and expression operators. Where a direct equivalent exists, a DQL example is provided. Where one doesn’t exist yet, the notes column says so β€” if any of those are important to your use case, reach out to support and let us know.

Filtering

$match β†’ WHERE

MongoDB
DQL
DQL supports the full comparison operator set: =, !=, <, >, <=, >=, BETWEEN, IN, NOT IN, LIKE, ILIKE, SIMILAR TO, IS NULL, IS NOT NULL, IS MISSING, IS NOT MISSING.
DQL

Projection

$project (include fields)

MongoDB
DQL

$project (computed fields)

MongoDB
DQL

$addFields / $set β€” add fields while keeping all existing ones

MongoDB
DQL
SELECT *, price * 1.1 AS adjusted_price FROM products is a syntax error. The wildcard must be qualified with the collection name: SELECT products.*, ....

$project (exclude fields) / $unset

MongoDB
DQL
SELECT *, MISSING field1 FROM products is a syntax error. The wildcard must be qualified with the collection name: SELECT products.*, MISSING field1 FROM products. See Excluding Fields.
Fields listed as MISSING override the wildcard expansion β€” products.* includes the field, but the explicit MISSING projection wins and the key is absent from output. For excluding a nested field within an object value, use object_unset:
DQL

Grouping and Aggregation

$group

MongoDB
DQL
All standard aggregates are supported: COUNT, SUM, AVG, MIN, MAX, MEDIAN, MID.

HAVING β€” filter on aggregated results

MongoDB
DQL

$addToSet / COUNT(DISTINCT ...)

DQL supports COUNT(DISTINCT ...) for counting unique values but does not accumulate values into an array within a group:
DQL
$push and $addToSet (as array accumulators in a group) have no DQL equivalent. If you need to collect values into an array per group, restructure the query or handle this in application code.

Sorting, Limiting, and Pagination

$sort, $limit, $skip

MongoDB
DQL

Counting

$count

MongoDB
DQL
With a filter:
DQL

Array Operations

$unwind β†’ array transform or search expression

MongoDB’s $unwind duplicates a document for each array element. DQL has no row-multiplying unwind. Instead:
  • Transform array contents in-place with the ARRAY expression
  • Filter documents based on array contents with ANY ... IN ... SATISFIES
MongoDB
DQL
DQL

$map β†’ ARRAY transformation expression

MongoDB
DQL

$filter β†’ ARRAY ... WHEN expression

MongoDB
DQL

$arrayElemAt β†’ subscript operator

MongoDB
DQL

$size β†’ array_length()

MongoDB
DQL

$in (array membership) β†’ array_contains()

MongoDB
DQL
$concatArrays and $slice have no documented DQL equivalent at this time.

Object Operations

$mergeObjects β†’ object_concat()

MongoDB
DQL
Later arguments win on key conflicts.

$objectToArray β†’ object_content()

MongoDB
DQL
object_content() returns an array of single-key objects. Available in SDK v4.11+. See Object Operators for full configuration options.

$arrayToObject β†’ OBJECT transformation expression

MongoDB
DQL
Available in SDK v5+.

Conditional Expressions

$cond β†’ CASE WHEN

MongoDB
DQL
Multi-branch:
DQL

$ifNull β†’ ifnull() / coalesce()

MongoDB
DQL
coalesce(v1, v2, ...) is a synonym that returns the first non-null, non-missing value.

$switch β†’ simple CASE

MongoDB
DQL

Bucketing

$bucket / $bucketAuto β†’ CASE + GROUP BY

MongoDB
DQL

Date Functions

DQL

String Functions

DQL

Type Inspection and Casting

$type β†’ type() / json_type()

MongoDB
DQL
type() returns DQL type names (boolean, string, integer, float, object, array, binary, null, missing). json_type() returns JSON type names (boolean, string, number, object, array, binary, null), returning "null" for MISSING values.

$toString, $toInt, $toDouble, etc. β†’ cast()

MongoDB
DQL
Supported target types: 'string', 'integer', 'float', 'boolean', 'binary'. Also accepts CAST(v AS type) SQL syntax. Returns MISSING if conversion is not possible.

Multi-Stage Pipelines β†’ Single SELECT

A MongoDB pipeline composing multiple stages maps to a single DQL SELECT:
MongoDB
DQL

Transaction Workarounds

Ditto’s transaction() API runs multiple DQL statements in a single atomic block at serializable isolation β€” either all statements commit or none do. This covers several patterns that have no direct DQL equivalent. Examples below use JavaScript; the same pattern applies in all supported SDKs.

Simulating Joins

DQL has no join syntax, but a read-only transaction guarantees both queries see the same consistent snapshot of the store. Combine the results in application code:
JavaScript

Consistent Multi-Query Snapshot

The equivalent of $facet β€” run independent aggregations that are guaranteed to reflect the same point in time:
JavaScript

Atomic Read-Modify-Write

The equivalent of $merge / $out β€” aggregate, transform in application code, then write the results back in the same atomic operation:
JavaScript

Known Gaps

The following MongoDB aggregation capabilities have no DQL equivalent.

Pipeline Stages

Grouping and Accumulators

Array Operations

String Operations

Math Functions

DQL provides abs(), ceil(), and floor(). The following have no equivalent: