Skip to content
Sealpaste

Delete all recent work?

This removes the 0 items saved in this browser. Your settings stay.

Stop saving recent work?

New work won't be saved in this browser. The 0 items already saved can stay or go.

SQL to MongoDB Converter

SQL

Paste SQL, or

CtrlV paste · Ctrl↵ run now

mongosh
Ready
Ln 1, Col 1
Runs in your browser. Nothing you paste is uploaded unless you share it.

Convert a SQL query to MongoDB: find(), aggregate() with $lookup and $group, or a write, for mongosh, Node.js or Python

Paste a SELECT, INSERT, UPDATE or DELETE. Get the MongoDB query that returns the same rows, and a plain note for anything MongoDB treats differently.

How to use it

  1. Paste one or more SQL statements, or open a .sql file.
  2. Choose mongosh to paste into the shell or Compass, or Node.js or Python for a program that runs it.
  3. Copy the result or download it. Differences from SQL, like $ne matching missing fields, are listed under it.

Examples

A filter, a sort and a limit become find()

Input
SELECT name, email FROM users WHERE email LIKE '%@example.com' AND age BETWEEN 18 AND 30 ORDER BY name LIMIT 20
Result
db.users.find({ email: /@example\.com$/, age: { $gte: 18, $lte: 30 } }, {
  name: 1,
  email: 1,
  _id: 0
}).sort({ name: 1 }).limit(20)

LIKE becomes a regular expression, anchored only where the pattern has no %; _id is left out because the query didn’t ask for it.

GROUP BY becomes aggregate()

Input
SELECT status, COUNT(*) AS orders, SUM(total) AS revenue FROM orders GROUP BY status ORDER BY revenue DESC
Result
db.orders.aggregate([
  {
    $group: { _id: '$status', orders: { $sum: 1 }, revenue: { $sum: '$total' } }
  },
  { $project: { _id: 0, status: '$_id', orders: 1, revenue: 1 } },
  { $sort: { revenue: -1 } }
])

The group key goes in _id and is renamed back in $project, so the documents have the same fields as the SQL rows.

What MongoDB can’t do the same way

Input
SELECT * FROM orders o RIGHT JOIN customers c ON o.customer_id = c._id
Result
RIGHT JOIN is not supported: MongoDB’s $lookup works from the left collection; swap the tables and use LEFT JOIN

What it converts

The simplest call that gives the same result is chosen: COUNT(*) alone becomes countDocuments(), one DISTINCT column becomes distinct(), a filter with a projection and a sort stays find(), and the rest is an aggregation pipeline. Numbers keep every digit, and DATE '2026-01-01' becomes a date, not a string.

  • SELECT with DISTINCT, aliases and computed columns (arithmetic, ||, CASE, UPPER, SUBSTRING, COALESCE, CAST, YEAR …).
  • WHERE with =, <>, <, >, IN, BETWEEN, LIKE and ILIKE, IS NULL, AND, OR and NOT, as a query filter wherever MongoDB has one, else $expr.
  • INNER and LEFT JOIN … ON a.x = b.y as $lookup and $unwind; GROUP BY with COUNT, COUNT(DISTINCT), SUM, AVG, MIN, MAX; HAVING; ORDER BY, LIMIT, OFFSET and FETCH FIRST.
  • INSERT … VALUES as insertOne or insertMany; UPDATE … SET as updateMany with $set, $inc and $mul (or an update pipeline when a value is computed from fields); DELETE as deleteMany.

What it refuses, and why

Subqueries, UNION, WITH, window functions (OVER), RIGHT, FULL and CROSS joins, and JOIN … USING have no single MongoDB call that returns the same rows, so they are named in an error rather than written wrong. Each message says what to do instead, for example running the inner query first, or $setWindowFields in the Aggregation Formatter .

Sources: MongoDB: SQL to MongoDB mapping chart, MongoDB: SQL to aggregation mapping chart

Where MongoDB differs from SQL

The query returns the same documents in nearly every case; these are the places it can differ, and the notes under the result name the ones your query has:

  • { field: null } matches a null value and a missing field; SQL’s IS NULL only the first.
  • $ne and $nin also match documents where the field is null or missing; SQL’s <> and NOT IN don’t.
  • Text comparison is case-sensitive unless the collection has a collation; ILIKE becomes a regular expression with the i option.

Questions

Does it run my query?
No. It reads the SQL and writes the MongoDB query; nothing connects to a database. Run the result in mongosh, Compass or your program to see documents.
Which SQL dialect does it read?
The common part of PostgreSQL, MySQL, SQL Server and SQLite: double quotes, backticks and [brackets] around names, LIMIT, OFFSET and FETCH FIRST, || and CONCAT, ILIKE, ::type casts and -- and /* */ comments.
Is my query uploaded?
No. Everything runs in your browser, so your query stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first. The conversion runs in a worker in this tab.

Related tools