Loading SQL to MongoDB Converter...
Please wait a moment

SQL to MongoDB Converter Online — Translate SQL Queries to MongoDB Syntax

Paste your SQL query above and it converts instantly — SELECT to find(), JOIN to $lookup, GROUP BY to aggregate pipeline. Everything runs in your browser, so your queries never leave your machine.

How to Convert SQL to MongoDB — Step-by-Step

Step 1

Input Your SQL Query

Have a SQL query you need to run in MongoDB? Three ways to get it in:

Paste directly: Copy your SQL query and paste it into the input editor — conversion happens instantly
Upload a file: Click "Upload" to select a .sql or .txt file from your computer
Load a sample: Click "Sample" to see an example with JOINs, GROUP BY, and aggregation

Example: SQL SELECT with WHERE Clause

Here is a typical SQL query with filtering conditions:

SELECT name, email, age
FROM users
WHERE status = 'active'
  AND age > 18
ORDER BY name ASC
LIMIT 20;
Step 2

Automatic Conversion to MongoDB

The converter runs the moment you paste — no button press needed. Here is what it produces:

MongoDB find() syntax: Simple SELECT queries become find() calls with query filters and projections
Chained methods: ORDER BY, LIMIT, and OFFSET become .sort(), .limit(), and .skip() method chains
Proper operator mapping: SQL operators are mapped to MongoDB query operators like $gt, $lt, $regex, $in, and $ne

Example: MongoDB find() Output

The same query, now as a MongoDB find() call:

db.users.find(
  { $and: [
    { status: "active" },
    { age: { $gt: 18 } }
  ] },
  { name: 1, email: 1, age: 1 }
)
  .sort({ name: 1 })
  .limit(20);
Step 3

Advanced Queries — GROUP BY & Aggregation

Complex SQL GROUP BY, HAVING, and aggregate functions are converted into MongoDB aggregation pipelines:

GROUP BY: Becomes a $group stage with _id set to the grouped fields
COUNT, SUM, AVG: Mapped to $sum, $avg, and other MongoDB accumulators
HAVING: Converted to a $match stage placed after the $group stage

Example: GROUP BY to $group Aggregation

SQL GROUP BY with COUNT:

SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;

Becomes this MongoDB aggregation pipeline:

db.employees.aggregate([
  { $group: {
    _id: "$department",
    emp_count: { $sum: 1 }
  } },
  { $match: { emp_count: { $gt: 10 } } }
]);
Step 4

Copy or Download Your MongoDB Query

When the conversion looks right, grab it:

Copy to clipboard: One-click copy to paste directly into MongoDB Shell, Compass, or your application code
Download as file: Save the converted query as a .js file for use in scripts or version control
Keep working: Use our other tools to work with your data — convert JSON formatting, JSON to CSV, or JSON to table

Frequently Asked Questions — SQL to MongoDB Converter

What SQL queries are supported?

The converter handles SELECT, INSERT INTO, UPDATE, and DELETE statements. For SELECT queries, it supports WHERE clauses with all common operators (=, >, <, >=, <=, !=, LIKE, IN, IS NULL, IS NOT NULL, BETWEEN), AND/OR conditions, JOIN and LEFT JOIN, GROUP BY, HAVING, ORDER BY, LIMIT, and OFFSET. Aggregate functions like COUNT, SUM, AVG, MIN, and MAX are also fully supported.

Does it handle SQL JOINs?

Yes. SQL JOIN and LEFT JOIN are converted to MongoDB $lookup aggregation stages. The converter automatically determines the correct localField and foreignField from your ON clause and adds $unwind stages to flatten the joined documents. LEFT JOINs use preserveNullAndEmptyArrays: true to preserve unmatched documents.

Can it convert subqueries?

The converter focuses on standard single-level SQL patterns — SELECT with JOINs, WHERE, GROUP BY, and aggregation functions. Deeply nested subqueries (SELECT inside SELECT) are not currently supported, but most real-world query patterns that developers need to migrate from SQL to MongoDB are handled correctly.

Is my data sent to a server?

No. All conversion happens entirely in your browser using client-side JavaScript. Your SQL queries are never transmitted to any server. This makes it safe to use with queries that contain sensitive table names, field names, or values.

Is this tool free to use?

Completely free, no account needed, no usage limits. Convert as many SQL queries as you want. The tool is designed for developers migrating from relational databases to MongoDB or learning MongoDB query syntax.

Does it support MongoDB aggregation pipeline?

Yes. Any SQL query that uses GROUP BY, HAVING, JOINs, or aggregate functions (COUNT, SUM, AVG, MIN, MAX) is automatically converted into a MongoDB aggregation pipeline with the appropriate stages — $lookup, $match, $group, $project, $sort, $skip, and $limit.

Does it handle INSERT, UPDATE, and DELETE?

Yes. INSERT INTO with VALUES is converted to insertOne() or insertMany() depending on the number of rows. UPDATE with SET and WHERE becomes updateMany() with $set. DELETE FROM with WHERE becomes deleteMany().