MongoDB Query to SQL
Translates MongoDB shell queries into SQL: find with operators like $in, $gt and $or, projection, sort, skip and limit, inserts, updates, deletes and simple aggregate pipelines for PostgreSQL, MySQL or SQLite.
Use the tool
Why this tool exists
People moving between document and relational databases keep running into the same translation problem. A filter document with $or, $in and a regular expression is compact, but the SQL equivalent takes time to write. This tool reads the shell query and writes SQL for the dialect you pick: $and and $or groups, $in, comparison operators, $regex as LIKE or REGEXP, $exists, projection, sort, skip and limit, plus counting, distinct, inserts, updates with $set, $inc and $unset, deletes and simple pipelines with $match, $group, $lookup and $project. The query is parsed as text and never executed. Real collections are rarely shaped like tables, so treat the SQL as a draft. It runs in your browser.
How to use it
- Paste a MongoDB shell query, such as db.users.find({...}), into the input box.
- Choose the SQL dialect: PostgreSQL, MySQL or SQLite.
- Copy the SQL and adjust table and column names to your schema.
Frequently asked questions
Which operations can it translate?
find and findOne, countDocuments, distinct, insertOne and insertMany, updateOne and updateMany, deleteOne and deleteMany, and simple aggregate pipelines.
How are regular expressions translated?
Simple patterns become LIKE where possible, and other patterns use the dialect's regular expression operator, such as REGEXP in MySQL. Check complex patterns by hand.
Does it run the query?
No. The shell query is only parsed as text and never executed, and no database is contacted at any point, so you can paste queries from production logs without any side effects.
What happens to nested documents?
Dotted paths and embedded documents have no direct SQL equivalent, so you may need to map them to columns or JSON operators yourself.