SQL to MongoDB Query Converter

Convert SQL queries to MongoDB find(), aggregate(), updateMany() and more, for mongosh or Mongoose.

Examples:
MongoDB query
Runs in your browser. Your queries are never uploaded.

How to convert SQL to a MongoDB query

  1. Paste a SQL query, or pick one of the examples above the editor.
  2. Choose mongosh for the shell and Compass, or Mongoose for Node.js code.
  3. Copy the result. You can paste several statements separated by semicolons.

The converter runs in your browser, so table names and values stay on your computer.

What SQL is supported

SQL MongoDB result
SELECT … WHERE … ORDER BY … LIMIT find(filter, projection).sort().skip().limit()
=, !=, >, >=, <, <= $eq, $ne, $gt, $gte, $lt, $lte
AND, OR, NOT Merged filters, $or, $nor
IN, NOT IN, BETWEEN $in, $nin, $gte + $lte
LIKE '%phone%' Regular expression /phone/i
IS NULL, IS NOT NULL null, { $ne: null }
COUNT(*) countDocuments()
SELECT DISTINCT col distinct()
GROUP BY with COUNT, SUM, AVG, MIN, MAX, HAVING aggregate() with $group, $project and $match
INNER JOIN / LEFT JOIN $lookup + $unwind
INSERT, UPDATE, DELETE insertOne/insertMany, updateMany with $set/$inc, deleteMany

SQL words and their MongoDB names

A table is a collection, a row is a document, and a column is a field. The SQL primary key id is _id in MongoDB, which is why the “Map id to _id” option is on by default. Text that looks like an ObjectId becomes ObjectId("…"), and date text becomes ISODate("…") so comparisons work on real dates.

Things to check after converting

LIKE becomes a regex. A pattern that starts with % can’t use an index, so it scans the whole collection. Prefer prefix searches such as 'abc%', or use a text or Atlas Search index for full-text search.

NULL behaves a little differently. { field: null } matches documents where the field is null and documents where the field doesn’t exist at all.

Think twice before copying JOIN-heavy designs. MongoDB can join with $lookup, but data that is always read together is usually better stored together in one document.

Frequently asked questions

Can it convert subqueries?

Not yet. Run the inner query first and paste its results into an IN (…) list, or rewrite it as a JOIN, which converts to $lookup.

Is the Mongoose output different from mongosh?

The filters are the same. The Mongoose version calls methods on a model such as User, adds await, and leaves ObjectId strings as plain strings because Mongoose converts them for you.

Is LIKE case-sensitive?

MySQL usually compares text without case, so the converter adds the i flag by default. Untick “Case-insensitive LIKE” for exact case matching.

logo

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome content in your inbox.

We don’t spam! Read our privacy policy for more info.

More free tools