PureDevTools

SQL to NoSQL Converter

Convert SQL statements to MongoDB shell commands — CREATE TABLE, SELECT, INSERT, UPDATE, DELETE, all in your browser

All processing happens in your browser. No data is sent to any server.
Examples:
MongoDB Output

MongoDB commands will appear here.

Quick Reference: SQL → MongoDB Mapping
SQLMongoDB
CREATE TABLEdb.createCollection() + $jsonSchema
SELECT *db.col.find({})
SELECT col1, col2db.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 }
ANDMerged 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 … VALUESdb.col.insertOne({}) / insertMany([])
UPDATE … SET … WHEREdb.col.updateMany(filter, { $set: {...} })
DELETE FROM … WHEREdb.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:

SQLMongoDB
CREATE TABLEdb.createCollection() with JSON Schema validator
SELECTdb.collection.find() with filter + projection
INSERT INTOdb.collection.insertOne() / insertMany()
UPDATEdb.collection.updateMany() with $set
DELETE FROMdb.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 TypeBSON Type
INT, INTEGER, SMALLINTint
BIGINTlong
FLOAT, DOUBLE, REALdouble
DECIMAL, NUMERICdecimal
VARCHAR, CHAR, TEXTstring
BOOLEAN, BOOLbool
DATE, DATETIME, TIMESTAMPdate
BLOB, BINARY, BYTEAbinData
UUIDstring
JSON, JSONBobject

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

SQLMongoDB
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

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:

OperatorDescription
$setSet field value
$unsetRemove a field
$incIncrement a numeric field
$pushAppend to an array
$pullRemove from an array
$addToSetAdd 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:

Related Tools

More Data Converter Tools