SQL to MongoDB Query Converter
Paste a SQL SELECT statement and get the equivalent MongoDB query: a find() call when the query is simple enough, or a full aggregation pipeline when it needs $lookup, $group, $match after grouping, or $project. The converter understands WHERE with AND/OR/NOT, comparison operators, IN, BETWEEN, IS NULL, LIKE and ILIKE (translated to anchored regular expressions), JOIN ... ON equality conditions, GROUP BY with COUNT, SUM, AVG, MIN and MAX, HAVING, ORDER BY, LIMIT and OFFSET. Output is available for mongosh, the Node.js driver and PyMongo. Anything that has no MongoDB equivalent — correlated subqueries, non-equality joins, window functions — is reported as an explicit warning instead of being silently dropped. Conversion runs entirely in your browser; your SQL never leaves the page.
Do more than sql to mongodb query converter — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Paste your SQL SELECT statement into the input box, or start from one of the sample queries.
- Pick an output style: auto uses find() for simple queries and an aggregation pipeline otherwise, or force a pipeline, mongosh, Node.js or PyMongo output.
- Read the warnings panel for any clause that has no MongoDB equivalent, then copy the generated query and run it against your collection.
Frequently asked questions
How do I convert a SQL JOIN to MongoDB?
A SQL JOIN becomes a $lookup stage in an aggregation pipeline, usually followed by $unwind. The table you join to becomes `from`, the column on the base table becomes `localField`, the column on the joined table becomes `foreignField`, and `as` is the array field the matched documents land in. $unwind with preserveNullAndEmptyArrays: true behaves like a LEFT JOIN; drop that option and it behaves like an INNER JOIN. Only equality joins map directly — a join on a range or an expression needs the pipeline form of $lookup with a `let` and a `$match` on `$expr`.
What is the MongoDB equivalent of GROUP BY and HAVING?
GROUP BY becomes a $group stage where `_id` holds the grouping key — a single field like "$country", or a document when you group by several columns. COUNT(*) becomes { $sum: 1 }, and SUM, AVG, MIN and MAX map to $sum, $avg, $min and $max on the field. HAVING becomes a second $match placed after the $group, because it filters the grouped results rather than the input documents. A final $project is normally added to lift the grouping key back out of _id so the output field names match the SQL column names.
Can every SQL query be converted to MongoDB?
No. Correlated subqueries, EXISTS, set operations like UNION and INTERSECT, recursive CTEs and window functions have no direct equivalent, and joins on anything other than equality need a hand-written pipeline-form $lookup. This tool flags those cases in the warnings panel rather than producing a query that looks right but is not. If you are moving between SQL and MongoDB regularly, Chat2DB connects to both and can generate and explain queries for either engine with AI — try it at https://app.chat2db.ai or download it from https://chat2db.ai/download.
