Loading MongoDB to SQL Converter...
Please wait a moment

MongoDB queries to SQL, with MongoDB's rules kept

Paste a MongoDB shell query above and read it back as SQL. The converter cares about the details that make a hand translation go wrong: $ne and $not keep rows where the field is empty, regexes become escaped LIKE patterns, values can't break out of their quotes, and long IDs keep every digit. Anything without an SQL equivalent is named in a note rather than quietly dropped. The query is read in your browser and never run.

How to Convert MongoDB to SQL — Step-by-Step

Step 1

Paste or upload a MongoDB query

Three ways to get your query in:

•
Paste it: mongosh syntax works as it is — unquoted keys, 'single quotes', ObjectId(), ISODate(), NumberLong() and /regex/. Several statements in one paste each get their own SQL statement.
•
Upload a file: Click "Upload" to pick a .js, .json or .txt file
•
Load the sample: Click "Sample" for an aggregate pipeline with $match, $lookup + $unwind, $group, $sort and $limit

Example: a find() with a filter, sort and limit

db.users.find(
  { status: "active", country: { $ne: "US" } },
  { name: 1, email: 1 }
).sort({ created_at: -1 }).limit(20)
Step 2

Read the SQL

There's no Convert button; the SQL updates a moment after you stop typing. The main mappings:

•
Collection to table: db.users becomes FROM users; odd names such as order-items are double-quoted
•
Filter to WHERE: $gt/$lte to comparisons, $in to IN, $or/$and to OR/AND, $mod to %
•
Negations keep empty fields: $ne, $nin, $not and $nor also match rows where the field is NULL, the way MongoDB matches documents without it
•
Projection, sort, paging: the column list (with _id unless you set _id: 0), ORDER BY, LIMIT and OFFSET

The SQL for the Step 1 query

SELECT _id, name, email
FROM users
WHERE status = 'active' AND (country IS NULL OR country <> 'US')
ORDER BY created_at DESC
LIMIT 20;
Step 3

Aggregations: GROUP BY, HAVING and JOIN

An aggregate pipeline becomes one SELECT. $group turns into GROUP BY with COUNT, SUM, AVG, MIN and MAX, a $match after it becomes HAVING, and $lookup followed by $unwind on the same field becomes an INNER JOIN (a LEFT JOIN with preserveNullAndEmptyArrays). A $lookup on its own gives each document an array of matches, which a flat row can't hold, so the converter asks for the $unwind. This is the SQL for the Sample pipeline:

SELECT users.country AS _id, COUNT(*) AS orders, COALESCE(SUM("order".total), 0) AS revenue
FROM users
INNER JOIN orders AS "order" ON users._id = "order".user_id
WHERE users.status = 'active' AND users.age >= 18
GROUP BY users.country
HAVING COUNT(*) >= 2
ORDER BY revenue DESC
LIMIT 10;
Step 4

Check the notes, then copy or download

•
Notes: Anything that has no exact SQL form, and any database-specific caveat, is listed in -- Note: lines above the statement it belongs to.
•
Copy or download: Copy the SQL or download it as converted-query.sql. Both always match the query that is in the editor right now.
•
Related tools: going the other way or to a different store? Try SQL to JSON, MongoDB to Elasticsearch or the MongoDB Query Formatter.

Frequently Asked Questions — MongoDB to SQL Converter

Which MongoDB methods does it convert?

find() and findOne() become SELECT (with .sort(), .skip() and .limit() as ORDER BY, OFFSET and LIMIT), countDocuments() becomes SELECT COUNT(*), distinct() becomes SELECT DISTINCT, insertOne() and insertMany() become INSERT, updateOne(), updateMany() and replaceOne() become UPDATE, and deleteOne() and deleteMany() become DELETE. aggregate() is converted for $match, $group, $sort, $skip, $limit, $project, $count and a $lookup followed by $unwind. You can paste mongosh syntax as it is: unquoted keys, single quotes, ObjectId(), ISODate(), NumberLong() and /regex/ all work, and the query is read, never run.

How are missing fields and null handled?

MongoDB keeps documents that don't have a field when you ask for $ne, $nin, $not or $nor, but in SQL a plain status <> 'active' drops every row where status is NULL. So the converter writes those conditions to keep NULL rows, for example (country IS NULL OR country <> 'US'). { field: null } becomes IS NULL. SQL can't tell a missing field from a stored null, so $exists becomes IS NOT NULL or IS NULL, and a note above the SQL says so.

What happens to operators that have no SQL form?

They are never dropped silently. In a SELECT, a condition such as $size, $elemMatch, $all, $type or a regex with groups is left out and named in a -- Note: line, so the query can only return more rows than MongoDB would, never fewer. In UPDATE and DELETE that would change rows MongoDB leaves alone, so the converter stops and tells you which part to rewrite. Array update operators ($push, $pull, $addToSet) and $rename get the same clear message.

How do regular expressions become SQL?

Plain text, ^, $, . and .* turn into LIKE with an escape character, so a literal % or _ in your pattern stays literal: /@company\.com$/ becomes email LIKE '%@company.com' ESCAPE '!'. The i flag becomes LOWER(column) LIKE with a lower-case pattern. Patterns with groups, character classes or quantifiers have no LIKE equivalent; they are named in a note, and PostgreSQL users can run them with the ~ operator instead. LIKE is case-sensitive in PostgreSQL but not in SQLite or MySQL by default, which the output also points out.

Can I paste values with quotes, or very long IDs?

Yes. Text values are written in single quotes with any quote doubled, so O'Brien becomes 'O''Brien' and a value like x'; DROP TABLE y; -- stays harmless text. Field and collection names that aren't plain words go in double quotes, and .limit() or .skip() must be whole numbers. Numbers are copied exactly as you typed them: 8901240544102066246 keeps every digit, 14.50 keeps its trailing zero, and the string "00501" stays '00501'. If a value contains a backslash, a note explains the MySQL setting that keeps it as typed.

Which SQL database is the output written for?

Standard SQL that runs as it is in PostgreSQL and SQLite; both were used to check the output. MySQL and MariaDB need ANSI_QUOTES for the double-quoted names and NO_BACKSLASH_ESCAPES when values hold backslashes, and updateOne() or deleteOne() use a subquery on _id that MySQL doesn't allow on the same table. Each of these cases adds a note at the top of the SQL. Nested fields such as address.city are written as columns named "address.city", so adjust them if your data sits in a JSON column.

Is my query sent anywhere?

No. The conversion runs in this page's JavaScript, in your browser. Nothing you paste or upload is sent to a server or kept, and a reload starts with an empty editor.