SQL to NoSQL

Paste a single SQL SELECT statement and the converter rewrites it as an equivalent MongoDB find() query. It maps the selected columns to a projection, the WHERE clause to a filter, ORDER BY to .sort() and LIMIT to .limit(). It is a query rewriter for simple statements, not a migration tool: JOIN, GROUP BY, aggregations and subqueries are not supported.

How the conversion works

  1. 1

    Parse the SELECT

    The tool reads a single statement of the form `SELECT columns FROM table [WHERE ...] [ORDER BY ...] [LIMIT n]`.

  2. 2

    Map each clause

    Columns become a projection `{ col: 1 }`, `WHERE` becomes a filter, `ORDER BY` becomes `.sort()` and `LIMIT` becomes `.limit()`.

  3. 3

    Translate the operators

    Comparison operators map to Mongo operators: `>` to `$gt`, `<` to `$lt`, `>=` to `$gte`, `<=` to `$lte`, `!=` to `$ne`, and `=` to a direct match.

  4. 4

    Copy the output

    The generated `db.collection.find(...)` line is ready to paste into the mongosh shell or a driver call.

A worked example

SQL:

SELECT name, price FROM products WHERE price > 100 ORDER BY price DESC LIMIT 5;

MongoDB query:

db.products.find({ price: { $gt: 100 } }, { name: 1, price: 1 }).sort({ price: -1 }).limit(5)

What the converter maps

SQL MongoDB find()
SELECT a, b projection { a: 1, b: 1 }
SELECT * all fields (empty projection)
FROM users db.users.find(...)
WHERE a = 5 { a: 5 }
WHERE a > 5 { a: { $gt: 5 } }
WHERE a >= 5 { a: { $gte: 5 } }
WHERE a != 5 { a: { $ne: 5 } }
WHERE a AND b { a: ..., b: ... }
WHERE a OR b { $or: [ ... ] }
ORDER BY a DESC .sort({ a: -1 })
LIMIT 10 .limit(10)

What is not supported

  • JOINs. Only a single table is read. Combining collections needs $lookup or a denormalized data model, which is a schema decision, not a query rewrite.
  • GROUP BY and aggregations. COUNT, SUM, AVG and GROUP BY require the aggregation pipeline (db.collection.aggregate([...])), which this tool does not generate.
  • Subqueries, CTEs and window functions. Rethink the model or split the work into stages; they cannot be expressed in a find().
  • IN, LIKE and BETWEEN. The WHERE parser handles the comparison operators =, >, <, >=, <=, != and <> only.
  • Mixed AND and OR. A WHERE clause is treated as all-AND or all-OR; do not combine both in one statement.
  • INSERT, UPDATE, DELETE, CREATE TABLE. Only SELECT is converted.

About the output

The result is always a single db.collection.find(filter, projection) line, optionally followed by .sort() and .limit(). It targets the MongoDB shell and drivers. It does not produce Firestore or Elasticsearch queries, and it does not choose indexes or optimize for performance. Test the query against your own data before relying on it.

Frequently Asked Questions

No. The output is always a MongoDB find() query for the mongosh shell or a driver. There is no target selector for other document stores.

No. Those need the MongoDB aggregation pipeline (aggregate([...])), which this tool does not generate. It only produces find() queries with a filter, projection, sort and limit.

No. Only a single FROM table is read. NoSQL stores favor embedding or referencing, so a join usually means a data-model change rather than a query rewrite.

No. Only SELECT statements are converted. Collections in MongoDB are schemaless by default, so there is no table definition to translate.

Related Tools

Tool available in other languages