MongoDB Query ↔ SQL Converter.
Translate mongo shell find() queries to SQL SELECT and SQL back to find() or aggregate().
Runs in your browserAbout MongoDB Query ↔ SQL Converter
The MongoDB ↔ SQL converter translates queries between the mongo shell and SQL in both directions, which helps when you migrate data, write a report against both stores, or explain a Mongo filter to someone who thinks in SQL. In Mongo to SQL mode, paste a db.collection.find() call with its filter, projection, sort(), skip() and limit(); the converter writes a SELECT with WHERE, ORDER BY, LIMIT and OFFSET. Operators map one to one where SQL has an equivalent: $gt to >, $in to IN, $ne null to IS NOT NULL, $or and $and to grouped OR and AND, and simple regular expressions such as /^abc/ to LIKE 'abc%'. countDocuments() becomes SELECT COUNT(*) and distinct() becomes SELECT DISTINCT. In SQL to Mongo mode, a single-table SELECT becomes find() with a projection, while GROUP BY with COUNT, SUM, AVG, MIN or MAX becomes an aggregate() pipeline with $match, $group, $project, $sort and $limit. For example, SELECT status, COUNT(*) AS orders FROM orders GROUP BY status turns into a $group stage with orders: { $sum: 1 }. Typical mistakes the notes call out: $exists has no exact SQL meaning because SQL cannot tell a missing field from NULL, nested fields like address.city need a JSON column or a join, and JOINs require $lookup, which this tool does not generate. ObjectId() and ISODate() values are written as string literals. Everything is parsed in your browser, so queries with real customer values stay on your machine.
How to Use
- Choose the direction and paste a mongo shell query or a SQL SELECT.
- Read the converted query and the notes about operators that have no exact equivalent.
- Copy the result into your database client and adjust column or field names if needed.
Frequently Asked Questions
Which mongo shell methods are supported?
find(), findOne(), countDocuments(), count() and distinct(), followed by sort(), skip() and limit(). Use db.getCollection('name') for collection names with dashes.
Can it convert JOINs?
No. A SQL JOIN needs an aggregation $lookup stage, which depends on how your documents are embedded or referenced. The converter reports an error instead of guessing.
How are regular expressions converted?
Patterns made of plain text with ^ and $ anchors become LIKE, with LOWER() on both sides for the i flag. Other patterns become PostgreSQL's ~ or ~* operator; use REGEXP for MySQL.
Is anything sent to a server?
No. Both directions run in your browser.
Related Tools
Convert a SELECT statement into Laravel Query Builder and Eloquent code.
Format and beautify SQL queries.
Pretty print, minify, and validate JSON.
Draw an entity-relationship diagram from CREATE TABLE statements or DBML and download it as SVG.
Visualize PostgreSQL and MySQL EXPLAIN plans as a tree with time, cost and row-estimate warnings.