From a SQL query to the MongoDB command that does the same thing
Moving from SQL to MongoDB usually means looking up the same few translations again and again. Paste the query above and you get mongosh code: find() for simple reads, an aggregation pipeline with $lookup and $group for joins and grouping, and the matching write methods for INSERT, UPDATE and DELETE. Where the two databases behave differently, a note above the code says so.
How to Convert SQL to MongoDB, Step by Step
Paste a SQL query
Write the query the way you would for a table. The table name becomes the collection, and a column like address.city is read as a path inside the document.
.sql or .txt file of up to 10 MBExample: a SELECT with WHERE, ORDER BY and LIMIT
SELECT name, email FROM users WHERE status = 'active' AND age > 18 ORDER BY name ASC LIMIT 20;
Read the mongosh code
A simple SELECT becomes one find() call: WHERE is the filter, the column list is the projection (with _id: 0 so you only get what you asked for), and ORDER BY, OFFSET and LIMIT become .sort(), .skip() and .limit().
The same query as mongosh code
db.users.find({ status: "active", age: { $gt: 18 } }, { _id: 0, name: 1, email: 1 })
.sort({ name: 1 })
.limit(20);Check the notes
GROUP BY, aggregates, DISTINCT and JOIN need an aggregation pipeline. The grouped columns go into _id of a $group stage, COUNT, SUM, AVG, MIN and MAX become accumulators, HAVING becomes a $match after the group, and a final $project gives each column its SQL name. The // Note: lines point out differences such as SUM returning 0 for an all-NULL group where SQL returns NULL.
GROUP BY with HAVING
SELECT department, COUNT(*) AS emp_count FROM employees GROUP BY department HAVING COUNT(*) > 5;
becomes:
db.employees.aggregate([
{ $group: { _id: "$department", emp_count: { $sum: 1 } } },
{ $match: { emp_count: { $gt: 5 } } },
{ $project: { _id: 0, department: "$_id", emp_count: 1 } }
]);Copy or download
Paste the code into mongosh or the shell tab in Compass. The notes are JavaScript comments, so they can stay in while you check them. Download saves the same text as mongodb-query.js.
Frequently Asked Questions
Which SQL does it understand?
SELECT with a column list or *, WHERE with =, <>, !=, <, <=, >, >=, IN, NOT IN, BETWEEN, LIKE, ILIKE, IS NULL and IS NOT NULL joined by AND, OR, NOT and brackets, INNER and LEFT JOIN on one equality, GROUP BY with COUNT, SUM, AVG, MIN and MAX, HAVING, DISTINCT, ORDER BY, LIMIT and OFFSET (also LIMIT 5, 10, TOP and FETCH FIRST). On the writing side it handles INSERT … VALUES, UPDATE … SET and DELETE. Several statements separated by ; give several commands.
How are NULLs handled?
The way SQL does it, for both a stored null and a missing field. In SQL a comparison with NULL is never true, so rating <> 3 skips rows without a rating. A plain $ne in MongoDB would return them, so the converter writes { rating: { $nin: [3, null] } } instead. NOT, NOT IN and NOT BETWEEN get the same care, and rating = NULL matches nothing, as in SQL (use IS NULL).
How do JOINs become $lookup?
Each JOIN … ON a.x = b.y becomes a $lookup stage with localField and foreignField, followed by $unwind. An INNER JOIN drops documents without a match and first skips documents whose key is null, because SQL never joins on NULL. A LEFT JOIN keeps them with preserveNullAndEmptyArrays. Columns of the joined table are read from the alias, so o.total becomes "$o.total". RIGHT, FULL and CROSS JOIN stop with a message.
What happens to SQL that MongoDB can't express?
It is never dropped quietly. In a SELECT, a condition such as LOWER(name) = 'x' or a comparison between two columns is left out and named in a // Note: line, so the query can only return more documents than the SQL, never fewer. In UPDATE and DELETE the same thing stops with an error instead, because leaving a condition out would change documents the SQL would not touch. Subqueries, UNION and window functions stop with a message.
How do LIKE patterns convert?
A pattern without wildcards becomes a plain equality. Anything else becomes an anchored regex: % becomes .*, _ becomes a single character, and characters a regex treats specially, such as . ( ) or *, are escaped. LIKE stays case-sensitive (as in PostgreSQL); write ILIKE to get the i flag, which matches how MySQL and SQL Server compare by default.
Are long IDs and decimals kept exactly?
Yes. Numbers are written from the text of your query, never through a JavaScript number, so an ICCID like 8901240544102066246 becomes NumberLong("8901240544102066246") instead of a rounded 8901240544102066000, 14.50 stays 14.50, and a quoted '00501' stays the string "00501". Text is written as escaped JavaScript strings, so quotes or ${…} in your data can't break the code.
Is my query sent anywhere?
No. The conversion runs in your browser, the query is not uploaded, and nothing is kept: a reload starts with an empty editor.
Going the other way? Try MongoDB to SQL. The same SQL can also become a Elasticsearch search or DynamoDB request, and the MongoDB query formatter tidies a query you already have.
Related Tools
MongoDB to SQL
Convert MongoDB queries and aggregation pipelines to equivalent SQL statements
SQL to DynamoDB
Convert SQL queries to AWS DynamoDB query and scan operations with PartiQL support
DynamoDB to SQL
Convert AWS DynamoDB query and scan operations to equivalent SQL statements
MongoDB Query Formatter
Format, beautify, and validate MongoDB queries with proper indentation and syntax highlighting
MongoDB to DynamoDB
Convert MongoDB queries to AWS DynamoDB query operations and vice versa
DynamoDB to MongoDB
Convert AWS DynamoDB operations to MongoDB queries with proper operator mapping