Skip to content

Menu

SQL to MongoDB

Convert SQL queries to MongoDB queries

Runs entirely in your browserDatabaseFree, no sign-up
Loading tool

01What is SQL to MongoDB?

You know the SQL you want to write, but the service now talks to MongoDB. Paste one statement here and you get the mongosh query that does the same job. It is a starting point you read and check, not a black box. Under the hood the SQL goes through a small parser first, so quoted strings, parentheses and AND/OR precedence come out right instead of being split on spaces. A plain SELECT on one table becomes db.collection.find() with a filter and a projection. MongoDB returns _id unless you exclude it, so the projection adds _id: 0 when you didn't select it. ORDER BY, OFFSET and LIMIT become chained .sort(), .skip() and .limit() calls. The WHERE clause maps onto the query operators in the MongoDB manual. Comparisons become plain equality, $ne, $lt, $gt, $lte and $gte. OR becomes $or, NOT becomes $nor, IN and NOT IN become $in and $nin, and BETWEEN becomes a $gte plus $lte pair. LIKE turns into an anchored $regex, with % as .* and _ as any single character. Comparing two columns uses $expr. Once a query has a JOIN, GROUP BY, an aggregate, DISTINCT or an alias, the tool switches to an aggregate() pipeline. An INNER or LEFT JOIN on a single equality becomes $lookup followed by $unwind. For LEFT JOIN the unwind keeps users with no orders. GROUP BY with COUNT, SUM, AVG, MIN or MAX becomes $group and $project, and HAVING becomes a $match after the group. Here's a quick one. SELECT u.name, o.total FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE o.total > 100 produces a $lookup from orders on id and user_id, an $unwind with preserveNullAndEmptyArrays: true, a $match on "o.total" and a $project. Writes work too. INSERT gives insertOne() or insertMany(), UPDATE gives updateMany() with $set, $inc for col = col + n, $mul and $currentDate for NOW(). DELETE gives deleteMany(). Leave out WHERE and the output starts with a comment warning that every document is affected. Some things are refused on purpose. RIGHT, FULL and CROSS JOINs, subqueries, UNION and expressions with no MongoDB operator return an error message rather than a wrong query. Two semantic gaps stay with you. IS NULL also matches documents where the field is missing. And $regex is case-sensitive, so a MySQL LIKE that ignored case will behave differently. Your SQL is converted inside the page and never sent to a database.

Backend developers moving a Node or Python service from MySQL or PostgreSQL to MongoDB who still draft queries in SQL first. The tool runs entirely in your browser, so your data never leaves your device.

02How to use SQL to MongoDB

  1. Type or paste a single statement into the SQL Query box, or load one of the examples.
  2. Read the result in the MongoDB Query panel. If part of the SQL can't be converted, the panel explains why instead.
  3. Compare field names with your real documents, since column names are copied as-is.
  4. Click Copy to put the query on your clipboard and run it in mongosh.
  5. Click the reset icon to clear the box and start again.

03Worked examples

Input: SELECT name, email FROM users WHERE age >= 21 AND country = 'US' ORDER BY name LIMIT 5
Result: db.users.find({ age: { $gte: 21 }, country: "US" }, { name: 1, email: 1, _id: 0 }) .sort({ name: 1 }) .limit(5);
_id: 0 is added because MongoDB returns _id unless you exclude it.
Input: SELECT * FROM products WHERE name LIKE 'Pro%'
Result: db.products.find({ name: { $regex: "^Pro" } });
A trailing % becomes an anchored prefix regex.
Input: SELECT status, COUNT(*) AS n FROM orders GROUP BY status
Result: db.orders.aggregate([ { $group: { _id: { status: "$status" }, n: { $sum: 1 } } }, { $project: { _id: 0, status: "$_id.status", n: 1 } } ]);
COUNT(*) becomes $sum: 1, and $project flattens the group key back into a status field.
Input: SELECT u.name, o.total FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE o.total > 100
Result: db.users.aggregate([ { $lookup: { from: "orders", localField: "id", foreignField: "user_id", as: "o" } }, { $unwind: { path: "$o", preserveNullAndEmptyArrays: true } }, { $match: { "o.total": { $gt: 100 } } }, { $project: { _id: 0, name: "$name", total: "$o.total" } } ]);
The join alias o becomes the $lookup field, so o.total is addressed as "o.total".
Input: UPDATE users SET logins = logins + 1, seen_at = NOW() WHERE id = 7
Result: db.users.updateMany( { id: 7 }, { $inc: { logins: 1 }, $currentDate: { seen_at: true } } );
col = col + 1 is recognised as $inc, and NOW() as $currentDate.
Input: DELETE FROM sessions
Result: // No WHERE clause: this deletes every document db.sessions.deleteMany({});
A missing WHERE gives an empty filter plus a warning comment.

04Common use cases

  • Porting the reporting queries of an Express service from MySQL to MongoDB one statement at a time
  • Seeing how a LEFT JOIN turns into $lookup plus $unwind before writing your first pipeline by hand
  • Drafting a quick filter for mongosh or Compass when the SQL version is already in your head
  • Checking a bulk UPDATE in Mongo syntax and catching a missing WHERE before it touches every document
  • Showing SQL-minded teammates in a code review how GROUP BY and HAVING map to $group and $match
  • Turning a seed script of INSERT statements into insertMany() calls for a test collection

05Tips

  • Types are kept from the SQL, so 30 stays a number and '30' stays a string. If your documents store ages as strings, the number filter matches nothing.
  • Always qualify join columns with aliases (ON u.id = o.user_id). That is how the converter decides which side is localField and which is foreignField.
  • Put an index on each $lookup foreignField. Without one, MongoDB scans the joined collection for every input document.
  • If you always read two tables together, consider embedding one in the other instead of keeping the $lookup.
  • LIKE '%term%' becomes an unanchored regex that can't use an index well, so expect it to be slow on big collections.

06Related concepts

MongoDB aggregation, $lookup, Query translation, NoSQL, mongosh.

Frequently Asked Questions

What is SQL to MongoDB?

SQL to MongoDB is a free, browser-based developer tool on AIDToolStack. Convert SQL queries to MongoDB queries It requires no installation or sign-up and works entirely in your browser.

Is SQL to MongoDB free to use?

Yes. No sign-up, no fees and no usage cap.

Does SQL to MongoDB store my data?

No. It runs in your browser; nothing is uploaded.

Cheat sheet

Reviews and commentsShow

Reviews

Sign in to leave a review.

Loading reviews…

Comments

Sign in to leave a comment.

Loading comments…

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe