SQL INPUT
MONGODB OUTPUT
// MongoDB query will appear here...
// Try an example

What is this SQL to MongoDB Converter?

This tool helps you translate your standard SQL (Structured Query Language) queries into the equivalent query syntax used by MongoDB, a popular NoSQL document database. It is designed for developers transitioning or working with both relational databases and MongoDB.

Enter your SQL query in the input box, and the corresponding MongoDB query will be generated below. Thanks to an advanced SQL parser, this tool can now handle more complex queries including basic aggregations and varied WHERE clauses.

This converter leverages an SQL parsing library to support a broader range of SQL operations:

SELECT

find() — Basic Query

Translates column selection and WHERE conditions into a MongoDB find() call with query filter and projection.

SELECT name, age FROM users WHERE age > 25 db.users.find( { age: { $gt: 25 } }, { name: 1, age: 1, _id: 0 } )
SELECT

sort(), limit(), skip()

ORDER BY becomes sort(), LIMIT becomes limit(), OFFSET becomes skip().

SELECT * FROM products ORDER BY price DESC LIMIT 10 OFFSET 20 db.products.find({}) .sort({ price: -1 }) .skip(20).limit(10)
AGGREGATE

GROUP BY → aggregate()

GROUP BY with aggregate functions (COUNT, SUM, AVG, MIN, MAX) is translated into a MongoDB aggregation pipeline.

SELECT country, COUNT(*) AS total FROM users GROUP BY country db.users.aggregate([ { $group: { _id: { country: "$country" }, total: { $sum: 1 } } } ])
AGGREGATE

HAVING → $match after $group

HAVING filters are translated into a second $match stage in the aggregation pipeline, applied after grouping.

SELECT category, AVG(price) AS avg_p FROM products GROUP BY category HAVING AVG(price) > 50 → pipeline with $group then $match
WHERE

LIKE → $regex

SQL LIKE patterns with % (any characters) and _ (single character) are converted to MongoDB regular expressions.

WHERE name LIKE '%mongo%' { name: { $regex: /.*mongo.*/ } } WHERE code LIKE 'IT_' { code: { $regex: /^IT.$/ } } NOT LIKE { field: { $not: /pattern/ } }
WHERE

IN, IS NULL, AND/OR

Full support for IN/$in, NOT IN/$nin, IS NULL/{$eq:null}, IS NOT NULL/{$ne:null}, AND/$and, OR/$or.

WHERE status IN ('a','b') { status: { $in: ["a","b"] } } WHERE deleted_at IS NULL { deleted_at: null } WHERE a=1 AND b=2 { $and: [{a:1},{b:2}] }
INSERT

insertOne() / insertMany()

Single-row INSERT becomes insertOne(). Multi-row INSERT becomes insertMany(). Column names become document field names.

INSERT INTO users (name, age) VALUES ('Alice', 30) db.users.insertOne({ name: "Alice", age: 30 })
UPDATE

updateMany() with $set

SQL SET clauses become MongoDB $set operators. The WHERE condition becomes the filter document.

UPDATE users SET status = 'active' WHERE id = 42 db.users.updateMany( { id: 42 }, { $set: { status: "active" } } )
DELETE

deleteMany()

DELETE FROM with a WHERE condition becomes deleteMany(). Use with care — no WHERE deletes all documents.

DELETE FROM sessions WHERE expires < '2024-01-01' db.sessions.deleteMany({ expires: { $lt: "2024-01-01" } })

SQL → MongoDB operator reference

SQL Operator MongoDB Operator Notes
=$eq (or implicit)Exact match
!= / <>$neNot equal
>$gtGreater than
>=$gteGreater than or equal
<$ltLess than
<=$lteLess than or equal
IN (...)$inValue in list
NOT IN (...)$ninValue not in list
LIKE '%x%'$regex: /.*x.*/% → .*, _ → .
NOT LIKE$not: /pattern/Negated regex
IS NULLfield: nullIncludes missing fields
IS NOT NULL$ne: nullField exists and is not null
AND$andAll conditions must match
OR$orAny condition must match
COUNT(*)$sum: 1In $group stage
SUM(f)$sum: "$f"In $group stage
AVG(f)$avg: "$f"In $group stage
MIN(f)$min: "$f"In $group stage
MAX(f)$max: "$f"In $group stage
ORDER BY ASCsort({ f: 1 })Ascending
ORDER BY DESCsort({ f: -1 })Descending
LIMIT nlimit(n)
OFFSET nskip(n)

Why Convert SQL to MongoDB?

Database Migration

Moving from MySQL, PostgreSQL or SQLite to MongoDB? This tool helps you translate your existing query logic quickly and accurately, reducing the risk of errors during migration.

Learning MongoDB

If you know SQL, this converter shows you exactly how each SQL concept maps to MongoDB's document model — making it the fastest way to learn MongoDB query syntax.

Daily Development

Even experienced MongoDB developers sometimes think in SQL first. Use this tool as a quick reference to translate your mental model into the correct MongoDB syntax.

Team Collaboration

Share converted queries with teammates who come from a SQL background. A common language reduces friction when working across different database ecosystems.