SQL to NoSQL Converter
Convert SQL statements to MongoDB shell commands — CREATE TABLE, SELECT, INSERT, UPDATE, DELETE, all in your browser
MongoDB commands will appear here.
Quick Reference: SQL → MongoDB Mapping
| SQL | MongoDB |
|---|---|
| CREATE TABLE | db.createCollection() + $jsonSchema |
| SELECT * | db.col.find({}) |
| SELECT col1, col2 | db.col.find({}, { col1: 1, col2: 1 }) |
| WHERE col = val | { col: val } |
| WHERE col > val | { col: { $gt: val } } |
| WHERE col BETWEEN a AND b | { col: { $gte: a, $lte: b } } |
| WHERE col IN (a, b) | { col: { $in: [a, b] } } |
| WHERE col LIKE '%x%' | { col: { $regex: /x/i } } |
| WHERE col IS NULL | { col: null } |
| AND | Merged into one filter object |
| OR | { $or: [...] } |
| ORDER BY col ASC | .sort({ col: 1 }) |
| ORDER BY col DESC | .sort({ col: -1 }) |
| LIMIT n | .limit(n) |
| OFFSET n | .skip(n) |
| INSERT INTO … VALUES | db.col.insertOne({}) / insertMany([]) |
| UPDATE … SET … WHERE | db.col.updateMany(filter, { $set: {...} }) |
| DELETE FROM … WHERE | db.col.deleteMany(filter) |
You’re migrating from PostgreSQL to MongoDB and have 50 SQL queries — SELECT, INSERT, UPDATE, DELETE with JOINs, WHERE clauses, and GROUP BY — that need to be rewritten as MongoDB shell commands. The syntax mapping isn’t obvious: WHERE age > 25 AND status = 'active' becomes { age: { $gt: 25 }, status: "active" }, and SELECT name, email FROM users becomes db.users.find({}, { name: 1, email: 1 }).
Why This Converter (Not the SQL Formatter)
PureDevTools has a SQL Formatter for formatting SQL queries. This tool translates SQL to MongoDB — paste CREATE TABLE, SELECT, INSERT, UPDATE, or DELETE statements and get equivalent MongoDB shell commands with JSON Schema validation, query filters, projections, sort, and limit. Everything runs in your browser.
What Is SQL to NoSQL Conversion?
When migrating a relational database to MongoDB or starting a new MongoDB project with a SQL background, it helps to understand how familiar SQL constructs map to MongoDB equivalents. SQL and MongoDB use fundamentally different data models — tables vs. collections, rows vs. documents, fixed schemas vs. flexible schemas — but the intent of most operations translates cleanly.
This tool converts the five core SQL statement types to their MongoDB counterparts:
| SQL | MongoDB |
|---|---|
CREATE TABLE | db.createCollection() with JSON Schema validator |
SELECT | db.collection.find() with filter + projection |
INSERT INTO | db.collection.insertOne() / insertMany() |
UPDATE | db.collection.updateMany() with $set |
DELETE FROM | db.collection.deleteMany() |
CREATE TABLE → createCollection
In SQL, CREATE TABLE defines a strict schema. In MongoDB, collections are schema-less by default, but you can enforce structure with JSON Schema validation:
CREATE TABLE users (
id INT NOT NULL PRIMARY KEY,
email VARCHAR(255) NOT NULL,
age INT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Converts to:
db.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["id", "email"],
properties: {
id: { bsonType: "int" },
email: { bsonType: "string" },
age: { bsonType: "int" },
created: { bsonType: "date" }
}
}
}
});
db.users.createIndex({ id: 1 }, { unique: true });
SQL Type to BSON Type Mapping
| SQL Type | BSON Type |
|---|---|
INT, INTEGER, SMALLINT | int |
BIGINT | long |
FLOAT, DOUBLE, REAL | double |
DECIMAL, NUMERIC | decimal |
VARCHAR, CHAR, TEXT | string |
BOOLEAN, BOOL | bool |
DATE, DATETIME, TIMESTAMP | date |
BLOB, BINARY, BYTEA | binData |
UUID | string |
JSON, JSONB | object |
SELECT → find()
SQL SELECT maps directly to MongoDB’s find() method:
SELECT name, email
FROM users
WHERE age > 18 AND status = 'active'
ORDER BY name ASC
LIMIT 10 OFFSET 20;
Converts to:
db.users.find(
{ age: { $gt: 18 }, status: "active" },
{ name: 1, email: 1 }
)
.sort({ name: 1 })
.skip(20)
.limit(10);
WHERE Clause Operators
| SQL | MongoDB |
|---|---|
col = val | { col: val } |
col != val | { col: { $ne: val } } |
col > val | { col: { $gt: val } } |
col >= val | { col: { $gte: val } } |
col < val | { col: { $lt: val } } |
col <= val | { col: { $lte: val } } |
col BETWEEN a AND b | { col: { $gte: a, $lte: b } } |
col IN (a, b, c) | { col: { $in: [a, b, c] } } |
col NOT IN (a, b) | { col: { $nin: [a, b] } } |
col LIKE '%text%' | { col: { $regex: /text/i } } |
col IS NULL | { col: null } |
col IS NOT NULL | { col: { $ne: null } } |
a AND b | { ...a, ...b } or { $and: [...] } |
a OR b | { $or: [a, b] } |
SELECT * vs Specific Columns
SELECT *→find({})with no projection (returns all fields)SELECT col1, col2→find({}, { col1: 1, col2: 1 })
Note: In MongoDB, projections are inclusive (1) or exclusive (0). You cannot mix 1 and 0 except to exclude _id.
INSERT INTO → insertOne / insertMany
Single-row inserts use insertOne(), multiple rows use insertMany():
INSERT INTO products (name, price, category)
VALUES ('Widget', 9.99, 'hardware');
db.products.insertOne({
name: "Widget",
price: 9.99,
category: "hardware"
});
Multi-row insert:
INSERT INTO products (name, price) VALUES ('A', 1.00), ('B', 2.00);
db.products.insertMany([
{ name: "A", price: 1 },
{ name: "B", price: 2 }
]);
UPDATE → updateMany / $set
SQL UPDATE maps to MongoDB’s updateMany() with the $set operator:
UPDATE orders
SET status = 'shipped', updated = NOW()
WHERE customer_id = 42;
db.orders.updateMany(
{ customer_id: 42 },
{ $set: { status: "shipped", updated: "NOW()" } }
);
Important: updateOne vs updateMany
MongoDB’s updateMany() updates all matching documents — equivalent to SQL’s UPDATE with no ROWNUM restriction. To update only the first match, use updateOne(). Unlike SQL, MongoDB doesn’t have an UPDATE LIMIT clause natively.
Other Update Operators
While this tool generates $set, MongoDB supports many more update operators:
| Operator | Description |
|---|---|
$set | Set field value |
$unset | Remove a field |
$inc | Increment a numeric field |
$push | Append to an array |
$pull | Remove from an array |
$addToSet | Add to array if not present |
DELETE FROM → deleteMany
DELETE FROM sessions WHERE expires_at < NOW();
db.sessions.deleteMany(
{ expires_at: { $lt: "NOW()" } }
);
A DELETE FROM with no WHERE clause deletes every document in the collection — the converter will warn you about this.
Key Differences: SQL vs MongoDB
Understanding these differences helps when migrating:
Schema flexibility: MongoDB documents in the same collection can have different fields. CREATE TABLE defines a contract; createCollection with $jsonSchema is optional enforcement.
Joins: SQL uses JOIN to combine tables. MongoDB doesn’t support multi-collection joins in find(). Use the $lookup aggregation stage instead (not shown by this converter).
Transactions: MongoDB 4.0+ supports multi-document ACID transactions, but the default is single-document atomicity.
Aggregations: Complex SQL like GROUP BY and HAVING requires MongoDB’s Aggregation Pipeline (db.collection.aggregate([...])), not find().
Auto-increment IDs: SQL often uses AUTO_INCREMENT integer IDs. MongoDB uses ObjectId by default for _id. If you need sequential integers, you’ll need a separate counter collection.
Frequently Asked Questions
Is my SQL data sent to a server?
No. All conversion logic runs entirely in your browser using JavaScript. No SQL statements or data are transmitted anywhere.
Does this support all SQL dialects?
The converter handles standard SQL syntax. It supports MySQL-style backtick quoting, PostgreSQL double-quote identifiers, and SQL Server bracket identifiers. Dialect-specific functions like NOW(), GETDATE(), or CURRENT_TIMESTAMP are passed through as string literals in the output.
What about JOINs and subqueries?
JOINs and subqueries are not supported by this converter because they require the MongoDB Aggregation Pipeline, which has a fundamentally different structure. The converter focuses on single-table CRUD operations.
What is the difference between updateOne and updateMany?
updateOne() modifies only the first document matching the filter. updateMany() modifies all matching documents. SQL’s UPDATE without a LIMIT clause is equivalent to updateMany(), which is what this converter generates.
Can I use the output directly in MongoDB Compass or Atlas?
Yes. The generated commands use standard MongoDB shell syntax (mongosh) and can be run in:
mongosh(MongoDB Shell)- MongoDB Compass → Mongosh tab
- MongoDB Atlas → Data Explorer shell
- Any MongoDB driver by adapting the syntax to your language’s API