SQL to MongoDB Query

Convert SQL SELECT statements to MongoDB find() and aggregate() queries with full clause support.

About This Tool

The SQL to MongoDB Query converter is a free browser-based tool that transforms SQL SELECT statements into equivalent MongoDB query syntax. Whether you are migrating from a relational database to MongoDB, learning MongoDB's query language, or simply need a quick reference for translating familiar SQL patterns, this tool generates both db.collection.find() and db.collection.aggregate() forms in seconds.

The converter parses your SQL and maps each clause to its MongoDB equivalent. Simple queries with WHERE, ORDER BY, and LIMIT are expressed using the concise find() syntax with filter documents, projections, and cursor modifiers. More advanced queries involving GROUP BY, aggregate functions (COUNT, SUM, AVG, MIN, MAX), or JOIN operations are automatically converted to MongoDB's aggregate() pipeline with stages like $match, $group, $sort, $lookup, and $unwind.

SQL comparison operators are mapped to MongoDB query operators: = becomes an exact match, > becomes $gt, >= becomes $gte, IN becomes $in, LIKE becomes $regex, and BETWEEN becomes a combined $gte / $lte filter. Logical operators AND and OR are translated to $and and $or array expressions. IS NULL and IS NOT NULL checks are converted to null comparisons.

SQL JOIN clauses are converted to MongoDB's $lookup aggregation stage, which performs a left outer join between collections. The tool generates the appropriate from, localField, foreignField, and as parameters, followed by an $unwind stage to flatten the results.

All processing happens entirely in your browser. Your SQL statements never leave your machine — there are no server round-trips, no logging, and no third-party services involved. This makes it safe to use with production queries and sensitive data structures.

How to Use

  1. Paste a SQL SELECT statement into the SQL Input panel on the left.
  2. The MongoDB query output updates automatically in the right panel as you type.
  3. For simple queries, toggle between find() and aggregate() output formats using the buttons above the panels.
  4. For queries with GROUP BY, JOIN, or aggregate functions, the tool automatically uses the aggregate() pipeline format.
  5. Click Copy or press Ctrl+Shift+C to copy the generated MongoDB query to your clipboard.
  6. Click Sample to load an example SQL query and see the conversion in action.
  7. Click Clear to reset both panels and start a new conversion.

Popular SQL to MongoDB Examples

View all SQL to MongoDB examples ->

FAQ

Which SQL statements are supported?

The tool supports SQL SELECT statements including SELECT, FROM, WHERE, JOIN (INNER, LEFT, RIGHT), GROUP BY, HAVING, ORDER BY, LIMIT, and OFFSET clauses. It also supports DISTINCT, column aliases, and aggregate functions (COUNT, SUM, AVG, MIN, MAX). INSERT, UPDATE, DELETE, and DDL statements are not supported.

How are SQL JOINs converted to MongoDB?

SQL JOIN clauses are converted to MongoDB's $lookup aggregation stage. An INNER JOIN produces a $lookup followed by $unwind, while a LEFT JOIN produces a $lookup followed by $unwind with preserveNullAndEmptyArrays set to true. The ON condition maps to localField and foreignField parameters.

When does the tool use find() vs aggregate()?

Simple queries with WHERE, ORDER BY, and LIMIT use the find() syntax, which is more concise and often faster. Queries involving GROUP BY, HAVING, aggregate functions (COUNT, SUM, etc.), or JOINs automatically use the aggregate() pipeline, which is required for these operations in MongoDB. You can also manually toggle between the two formats for simple queries.

How is SQL LIKE converted to MongoDB?

SQL LIKE patterns are converted to MongoDB $regex expressions. The % wildcard becomes .* (match any characters) and the _ wildcard becomes . (match any single character). The regex is case-insensitive by default, matching the common behavior of SQL LIKE in many databases.

Does it support subqueries or nested SELECTs?

Currently, the tool supports single-level SELECT statements. Nested subqueries, CTEs (WITH clauses), and UNION operations are not supported. For complex queries, consider breaking them into separate conversions and combining the MongoDB operations manually.

Is my data safe?

Yes. All parsing and query generation runs entirely in your browser using JavaScript. No data is sent to any server. You can verify this by checking the Network tab in your browser's developer tools while using the tool.

How are NULL checks handled?

SQL IS NULL is converted to a MongoDB filter checking for null (e.g., { field: null }), and IS NOT NULL is converted to { field: { $ne: null } }. These checks match documents where the field is either null or does not exist.

Related Tools