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

这个SQL到MongoDB转换器是什么?

此工具可帮助您将标准SQL(结构化查询语言)查询转换为MongoDB(一种流行的NoSQL文档数据库)使用的等效查询语法。它专为正在转型或同时使用关系数据库和MongoDB的开发人员设计。

在输入框中输入您的SQL查询,相应的MongoDB查询将在下方生成。得益于先进的SQL解析器,此工具现在可以处理更复杂的查询,包括基本聚合和各种WHERE子句。

此转换器利用SQL解析库来支持更广泛的SQL操作:

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)

为什么要将SQL转换为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.