Convert a MongoDB query to SQL

Translate a MongoDB filter or db.collection.find() call into a SQL condition, WHERE clause, or full SELECT — in ANSI, PostgreSQL, MySQL, or SQL Server.

Try:
SQL

What this tool does

Paste a MongoDB filter document or a db.collection.find(...) shell query and this tool converts it into deterministic SQL. It can emit a bare boolean condition, a WHERE clause, or a full SELECT statement with projection, sort, limit, skip, and count handling.

The parser accepts common Mongo shell syntax: unquoted keys, single quotes, trailing commas, comments, regular-expression literals, ObjectId(), ISODate(), new Date(), numeric helpers such as NumberLong(), and MongoDB Extended JSON wrappers like $oid and $date.

Worked example

Input:

db.orders.find(
  { status: { $in: ["paid", "shipped"] }, total: { $gt: 100 } },
  { _id: 0, orderId: 1, total: 1 }
).sort({ total: -1 }).limit(10).skip(20)

With Output set to select, the ANSI SQL output is:

SELECT "orderId", "total"
FROM "orders"
WHERE "status" IN ('paid', 'shipped') AND "total" > 100
ORDER BY "total" DESC
LIMIT 10 OFFSET 20;

Options

Supported operators and limits

The translator supports $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $and, $or, $nor, field-level $not, $exists, $regex, $mod, and $size where the SQL dialect has a safe equivalent. Unsupported operators explain why rather than guessing.

Input is limited to 100,000 characters and nesting is limited to 64 levels. Aggregation pipelines, writes, $lookup, $group, $elemMatch, $all, geo queries, text search, and schema-dependent array rewrites are intentionally not translated because they need collection schema knowledge.

FAQ

Can this convert aggregation pipelines?

No. A $match stage can often be pasted as a normal find filter, but stages such as $group, $lookup, and $unwind require schema and join decisions that are not present in a MongoDB snippet. The tool rejects pipelines instead of inventing a misleading SQL query.

How are dotted fields handled?

By default profile.city is treated as one SQL column name. If your document is stored in a JSON column, set Dotted paths to json; PostgreSQL uses ->> with casts, MySQL uses JSON_UNQUOTE(JSON_EXTRACT(...)), and SQL Server/ANSI use JSON_VALUE(...).

Does regular expression output work in every SQL dialect?

PostgreSQL and MySQL have regex operators, so most simple patterns can be emitted there. ANSI SQL and SQL Server do not have a portable regex operator, so only plain anchored patterns can become LIKE; complex patterns return an error that suggests switching dialects.

Will the generated SQL be parameterized?

No. The output is designed to be readable and pasteable. If you use it in application code, replace literal values with your database driver's placeholders and bind parameters before running it against real data.

Developer & Automation Access

Run it from the terminal

Same engine as this page, headless — via the gizza CLI:

gizza tool mongodb-query-to-sql 'db.users.find({ age: { $gte: 21 }, status: "active" })'

New to the CLI? Get gizza →

Open it by URL

Pre-fill and auto-run this tool with query parameters — the names match the API/CLI:

https://gizza.ai/tools/mongodb-query-to-sql/?query=db.users.find%28%7B%20age%3A%20%7B%20%24gte%3A%2021%20%7D%2C%20status%3A%20%22active%22%20%7D%29&output=where&dialect=ansi&table=sales.orders&nested=column&quote_identifiers=true&rename_id=true

Machine-readable descriptor: tool.json — title + parameters JSON Schema for agents.