SQL to MongoDB query converter

Paste a SQL SELECT, INSERT, or DELETE statement and get the equivalent MongoDB command — find(), aggregate() for GROUP BY or JOIN, insertOne()/insertMany(), or deleteMany(). Runs entirely in your browser — nothing is sent anywhere.

Try an example:

Worked examples

Filter, projection, sort

SELECT id, status, total
FROM orders
WHERE status = 'shipped' AND total > 100
ORDER BY total DESC

↓ converts to

db.orders.find({
  "status": "shipped",
  "total": {
    "$gt": 100
  }
}, {
  "id": 1,
  "status": 1,
  "total": 1
})
  .sort({"total": -1})

AND conditions on different fields merge into one filter document. The SELECT column list becomes a MongoDB projection (1 = include). MongoDB won't rename id to _id — field names are used as-is.

GROUP BY + HAVING

SELECT status, SUM(total) AS revenue
FROM orders
WHERE status != 'cancelled'
GROUP BY status
HAVING revenue > 1000

↓ converts to

db.orders.aggregate([
  {
    "$match": {
      "status": {
        "$ne": "cancelled"
      }
    }
  },
  {
    "$group": {
      "_id": "$status",
      "status": {
        "$first": "$status"
      },
      "revenue": {
        "$sum": "$total"
      }
    }
  },
  {
    "$match": {
      "revenue": {
        "$gt": 1000
      }
    }
  }
])

WHERE becomes a leading $match (filters before grouping). GROUP BY becomes $group with the group field as _id. Plain GROUP BY columns use $first to carry the value forward. HAVING becomes a second $match after $group.

INNER JOIN (equality condition)

SELECT *
FROM orders o
JOIN customers c ON o.customerId = c.id
WHERE o.status = 'shipped'

↓ converts to

db.orders.aggregate([
  {
    "$lookup": {
      "from": "customers",
      "localField": "customerId",
      "foreignField": "id",
      "as": "c"
    }
  },
  {
    "$unwind": "$c"
  },
  {
    "$match": {
      "status": "shipped"
    }
  }
])

$lookup joins the customers collection and places matches in an array under the alias c. $unwind flattens it — for an INNER JOIN, documents with no match are dropped. For a LEFT JOIN, $unwind uses preserveNullAndEmptyArrays: true to keep unmatched base rows. All column references in a JOIN query must be qualified with the table alias.

Semantic differences from SQL

  • Null and missing fields: IS NULL maps to { field: null }, which matches both an explicit null value and documents where the field is absent. SQL IS NULL only matches explicit nulls in a typed schema.
  • Data types: MongoDB stores typed values (string, number, date, ObjectId…). Comparing a string field with a number returns no matches — there is no implicit type coercion.
  • Projection vs SELECT: MongoDB projection includes or excludes fields but cannot rename them without an aggregation pipeline. A column alias like SELECT id AS orderId is accepted and silently ignored — the output field is still named id.
  • UNION deduplication: UNION over the same table is rewritten as a single $or. A document matching both branches appears once — equivalent to SQL UNION DISTINCT, not UNION ALL.
  • Array fields: A filter like { tags: "mongodb" } matches documents where tags equals the string or is an array containing it. This converter does not add array-specific operators.

What v1 supports

Supported

  • SELECT column list or * — an alias on a plain column (AS or bare) is accepted and ignored, since Mongo can't rename a field without an aggregation pipeline
  • WHERE with =, !=, <, >, <=, >=
  • AND / OR / NOT, with parentheses
  • IN, NOT IN
  • LIKE / NOT LIKE (% and _ wildcards)
  • BETWEEN / NOT BETWEEN
  • IS NULL / IS NOT NULL
  • ORDER BY, LIMIT, OFFSET
  • GROUP BY, HAVING
  • COUNT, SUM, AVG, MIN, MAX (aliased with AS)
  • INSERT (single or multi-row, explicit column list)
  • DELETE, with or without WHERE
  • UNION / UNION DISTINCT over the same table
  • INNER JOIN, LEFT [OUTER] JOIN (one join, equality condition, qualified columns)

Not supported

  • RIGHT / FULL / CROSS JOIN, or more than one JOIN
  • Subqueries — same problem as multi-table JOINs
  • UNION across different tables — needs $unionWith
  • JOIN combined with GROUP BY or aggregate functions
  • UNION ALL — could produce duplicate rows, which a single find() can't represent
  • UPDATE statements
  • INSERT without an explicit column list — cannot know the field names if not provided

The converter maps SQL syntax to the closest MongoDB equivalent — it does not know your schema. It won't rename id to _id, and a WHERE clause on a field that doesn't exist in your documents will simply return no matches. A DELETE with no WHERE clause converts to deleteMany({}), which removes every document — the generated command calls this out, but always double-check destructive commands before running them against real data. With a JOIN, every column reference (SELECT, WHERE, ORDER BY) must be qualified with its table alias (e.g. o.status, c.name) since there's no way to guess which table an unqualified name belongs to.