SQL to MongoDB
ConvertersTranslate SQL SELECT statements into equivalent MongoDB find() queries and aggregation pipelines. Handles WHERE, JOIN, GROUP BY, ORDER BY, and LIMIT with step-by-step pipeline explanation.. Free, private — all processing in your browser.
SQL and MongoDB query languages look different but express many of the same ideas. WHERE becomes $match, GROUP BY becomes $group, JOIN becomes $lookup, ORDER BY becomes $sort. Developers moving between the two regularly rediscover this mapping and spend time translating queries by hand. This converter automates the translation so you can paste SQL and get the MongoDB equivalent — either a find() call for simple reads or an aggregation pipeline for anything more complex.
The tool handles the core of SQL: SELECT with projections, WHERE with boolean operators, JOIN (inner, left, right, full), GROUP BY with HAVING and aggregate functions (COUNT, SUM, AVG, MIN, MAX), ORDER BY with multiple columns and directions, LIMIT and OFFSET, and subqueries where possible. The output is a MongoDB query or pipeline formatted for the mongo shell, which you can paste directly into MongoDB Compass or your Node.js driver code.
Beyond the translation, the tool explains each stage of the resulting pipeline. A SQL JOIN becomes a $lookup stage plus $unwind plus $match, and seeing that decomposition helps you understand why MongoDB needs more stages and how the data flows. Edge cases are flagged explicitly: correlated subqueries, full outer joins, and window functions have no direct MongoDB equivalent in older versions and may require manual restructuring or MongoDB 5.0+ features. For a developer migrating an app or a team learning aggregation pipelines, this converter is a starting point that saves hours of research and trial-and-error.
SQL to MongoDB — key features
SELECT with projection
Translates SELECT columns to $project or find() projection.
WHERE with boolean logic
AND, OR, NOT, IN, BETWEEN, LIKE all map to $match operators.
JOIN types
INNER, LEFT, RIGHT, and FULL joins translate to $lookup with correct behavior.
GROUP BY and aggregates
COUNT, SUM, AVG, MIN, MAX become $group accumulators.
ORDER BY and LIMIT
Multi-column ORDER BY and LIMIT/OFFSET translate to $sort, $limit, $skip.
Pipeline stage explanation
Each aggregation stage gets a plain-English description so you understand the flow.
Unsupported feature warnings
Flags window functions, correlated subqueries, and other SQL features without direct MongoDB equivalents.
How to use the SQL to MongoDB
- 1
Paste your SQL
Drop in the SELECT statement you want to translate. The parser understands standard SQL syntax.
- 2
Review the parsed output
The tool shows the SQL it understood. Fix any parse errors before conversion.
- 3
Pick find or aggregation
Simple queries with single table and basic filters become find(). Anything with JOIN, GROUP BY, or nested logic becomes aggregation.
- 4
Read the stage explanation
Each aggregation stage is labeled with what it does. Useful for learning why MongoDB needs the structure it does.
- 5
Copy the MongoDB query
Paste into mongo shell, Compass, Mongoose, or the official driver in your language.
Common use cases for the SQL to MongoDB
Migration
- →:
- →:
- →:
Learning
- →:
- →:
- →:
Data engineering
- →:
- →:
- →:
Performance comparison
- →:
- →:
- →:
SQL to MongoDB — examples
Simple SELECT
Single table filter
SELECT * FROM users WHERE age > 18
db.users.find({age: {$gt: 18}})JOIN
Two-table inner join
SELECT u.name, o.total FROM users u JOIN orders o ON u.id=o.user_id
pipeline with $lookup + $unwind + $project
GROUP BY
Sum by category
SELECT category, SUM(price) FROM items GROUP BY category
pipeline with $group
ORDER BY LIMIT
Top 10 latest
SELECT * FROM posts ORDER BY created_at DESC LIMIT 10
find().sort({created_at: -1}).limit(10)Complex
JOIN + GROUP + HAVING
aggregate per user
multi-stage aggregation pipeline
Technical details
SQL is declarative: you describe what you want and the optimizer figures out how to retrieve it. MongoDB's query language is closer to a pipeline: you chain stages that transform documents from the collection toward the desired result. The translation is not always straightforward because the two models solve the same problem differently.
Key mappings:
- SELECT fields: becomes $project stage in aggregation or a projection document in find()
- WHERE: becomes $match stage or find filter
- JOIN: becomes $lookup stage, with $unwind to flatten arrays and optional $match to filter
- GROUP BY: becomes $group with _id set to the grouping key
- HAVING: becomes $match after $group
- Aggregate functions (COUNT, SUM, AVG): become $sum, $avg, $min, $max in $group accumulators
- ORDER BY: becomes $sort
- LIMIT/OFFSET: become $limit and $skip
- DISTINCT: becomes $group with _id on the distinct fields
Differences that require care:
- MongoDB has no schema-enforced joins; $lookup joins by explicit field matching
- SQL's NULL handling differs from MongoDB's handling of missing fields versus null values
- SQL's CASE WHEN becomes $cond or $switch in aggregation
- Subqueries translate to nested pipelines or multiple $lookup stages
- SQL's EXISTS becomes $match with an array size check after $lookup
For complex SQL with window functions or CTEs, MongoDB 5.0+ supports $setWindowFields and views provide CTE-like functionality. Older MongoDB versions may require application-level workarounds. The converter flags these and suggests alternatives where possible.
Common problems and solutions
⚠Schema assumptions
SQL assumes typed columns; MongoDB is schemaless. A query that works on well-typed data may behave oddly on inconsistent documents.
⚠NULL vs missing
SQL NULL is distinct from missing column. MongoDB has both null values and missing fields; $match queries must distinguish.
⚠Index differences
Translated queries may not be optimal without MongoDB-specific indexes. Create indexes on filter and sort fields.
⚠Correlated subqueries
Some correlated subqueries have no clean MongoDB equivalent. The tool flags these.
⚠Transactions
SQL transactions span multiple statements; MongoDB transactions have different semantics and require explicit sessions.
⚠Window functions
SQL window functions require MongoDB 5.0+ $setWindowFields. Older MongoDB versions need application-level computation.
SQL to MongoDB — comparisons and alternatives
Manual SQL-to-MongoDB translation is error prone and slow, especially for complex JOINs and aggregations. MongoDB documentation covers aggregation pipelines well but doesn't start from SQL knowledge. ORMs like Prisma can generate MongoDB queries from schema but not from SQL source. This tool is purpose-built: paste SQL, get MongoDB. Works offline in the browser, explains each stage, and flags SQL features that don't translate cleanly. Best for migration work, learning aggregation pipelines, and teams moving from relational databases to MongoDB.
Frequently asked questions about the SQL to MongoDB
▶Does this produce optimal MongoDB queries?
It produces semantically-equivalent queries. Optimization may require MongoDB-specific indexes and manual refactoring for your data.
▶Can it handle stored procedures?
Not directly. Stored procedures often encode business logic that needs manual translation to application code or MongoDB functions.
▶What about views and materialized views?
SQL views translate to MongoDB views (read-only aggregation pipelines). Materialized views translate to collections populated by an aggregation job.
▶Does it support all SQL dialects?
Standard SQL (SELECT, JOIN, WHERE, GROUP BY, ORDER BY) works. Dialect-specific features (PostgreSQL arrays, MySQL hints) may need manual handling.
▶Can I use this for real migrations?
Yes as a starting point. Always test translated queries against real data and tune indexes for MongoDB's execution model.
▶What MongoDB version is assumed?
The converter targets MongoDB 5.0+ for modern features but notes compatibility for older versions.
▶Is Mongoose syntax supported?
Output is mongo shell syntax. Most is compatible with Mongoose queries with minor adjustments to collection access.
▶Can I convert MongoDB back to SQL?
Reverse translation is theoretically possible but this tool focuses on SQL-to-MongoDB direction.
Additional resources
Related tools
All ConvertersCSV to SQL
Convert CSV data to SQL INSERT statements with auto type detection, escaping, and configurable table name and SQL dialect.
JSON Formatter
Format, validate, and beautify JSON instantly in your browser
JSON to SQL
Generate SQL INSERT statements and CREATE TABLE from JSON arrays
JSON to TypeScript
Generate TypeScript interfaces and types from JSON — handle nested, optional, arrays
JSON to XML Converter
Convert between JSON and XML — for SOAP APIs, legacy integration, data migration
JSON Validator
Validate JSON syntax and schema — pinpoint errors by line and column
Learn more
Explore more tools
200+ free tools that run in your browser.
Browse all tools →