Free tool

SQL to MongoDB converter

Paste a SQL SELECT and get the MongoDB equivalent: a find() for simple filters, or an aggregation pipeline for GROUP BY, HAVING, DISTINCT and JOINs. Each clause is mapped and explained, including where SQL and MongoDB behave differently.

  • No signup
  • Gemini translates it, the read-only guard checks it
  • 13 verified examples
0/2000·orders

SQL SELECT statement. Press ⌘ + Enter to run.

552/20,000

JSON or mongosh syntax. ObjectId(...) and ISODate(...) are fine. Replace real values; never paste credentials.

13 worked examples,
run against real data

Every example ran on the sample orders, customers, tickets, products collections before it was published, and the table under it is what MongoDB returned. Load any of them into the tool above.

Worked examples
  • findorders01

    WHERE, ORDER BY, LIMIT

    SELECT * FROM orders WHERE status = 'delivered' ORDER BY createdAt DESC LIMIT 10;

    db.orders.find({ status: "delivered" })
      .sort({ createdAt: -1 })
      .limit(10)
    • WHERE becomes the filter object.
    • ORDER BY ... DESC becomes sort({ createdAt: -1 }).
    Result of WHERE, ORDER BY, LIMIT on the sample data
    orderNumbertotal
    A-10019097
    A-100312999
  • findcustomers02

    Selected columns with IN

    SELECT name, email FROM customers WHERE plan IN ('pro', 'business') ORDER BY name;

    db.customers.find({ plan: { $in: ["pro", "business"] } }, { _id: 0, name: 1, email: 1 })
      .sort({ name: 1 })
    • The column list becomes a projection; _id is returned by default, so it is excluded explicitly.
    • IN (...) maps to $in.
    Result of Selected columns with IN on the sample data
    nameemail
    Aarav Mehtaaarav@example.com
    Priya Nairpriya@example.com
    Sofia Rossisofia@example.com
  • aggregatetickets03

    COUNT(*) with WHERE

    SELECT COUNT(*) AS open_tickets FROM tickets WHERE status = 'open';

    db.tickets.aggregate([{ $match: { status: "open" } }, { $count: "open_tickets" }])
    • WHERE becomes $match.
    • COUNT(*) AS open_tickets becomes $count with the alias as the field name.
    Result of COUNT(*) with WHERE on the sample data
    open_tickets
    2
  • aggregateorders04

    GROUP BY with SUM

    SELECT status, SUM(total) AS revenue FROM orders GROUP BY status ORDER BY revenue DESC;

    db.orders.aggregate([
      { $group: { _id: "$status", revenue: { $sum: "$total" } } },
      { $project: { _id: 0, status: "$_id", revenue: 1 } },
      { $sort: { revenue: -1 } }
    ])
    • GROUP BY status becomes $group with _id: "$status".
    • SUM(total) becomes { $sum: "$total" }.
    Result of GROUP BY with SUM on the sample data
    statusrevenue
    shipped37998
    pending32496
    delivered22096
    cancelled1299
  • aggregateorders05

    GROUP BY with HAVING

    SELECT paymentMethod, AVG(total) AS avg_total FROM orders GROUP BY paymentMethod HAVING COUNT(*) > 1;

    db.orders.aggregate([
      {
        $group: { _id: "$paymentMethod", avg_total: { $avg: "$total" }, n: { $sum: 1 } }
      },
      { $match: { n: { $gt: 1 } } },
      { $project: { _id: 0, paymentMethod: "$_id", avg_total: 1 } }
    ])
    • HAVING is a $match placed after the $group, filtering on the computed count.
    • The helper count n is dropped in the final $project.
    Result of GROUP BY with HAVING on the sample data
    paymentMethodavg_total
    card27831
  • findproducts06

    LIKE with a prefix

    SELECT sku, name FROM products WHERE name LIKE 'USB%';

    db.products.find({ name: { $regex: "^USB" } }, { _id: 0, sku: 1, name: 1 })
    • LIKE 'USB%' becomes the anchored regex ^USB, which can use an index on name.
    • MongoDB regexes are case-sensitive by default, unlike LIKE under many SQL databases' default collations.
    Result of LIKE with a prefix on the sample data
    skuname
    CB-050USB-C cable, 2 m
  • aggregateproducts07

    SELECT DISTINCT

    SELECT DISTINCT category FROM products ORDER BY category;

    db.products.aggregate([{ $group: { _id: "$category" } }, { $sort: { _id: 1 } }])
    • A $group with only an _id returns each distinct value once.
    • db.products.distinct("category") gives the same values as a plain array.
    Result of SELECT DISTINCT on the sample data
    _id
    accessories
    audio
    displays
  • aggregateorders08

    INNER JOIN

    SELECT o.orderNumber, o.total, c.name FROM orders o JOIN customers c ON o.customerId = c._id WHERE o.total > 10000 ORDER BY o.total DESC;

    db.orders.aggregate([
      { $match: { total: { $gt: 10000 } } },
      {
        $lookup: {
          from: "customers",
          localField: "customerId",
          foreignField: "_id",
          as: "c"
        }
      },
      { $unwind: "$c" },
      { $project: { _id: 0, orderNumber: 1, total: 1, name: "$c.name" } },
      { $sort: { total: -1 } }
    ])
    • The WHERE on the orders side moves into an early $match.
    • JOIN ... ON becomes $lookup with localField and foreignField.
    Result of INNER JOIN on the sample data
    orderNumbertotalname
    A-100237998Priya Nair
    A-100532496Priya Nair
    A-100312999Sofia Rossi
  • aggregatecustomers09

    LEFT JOIN with COUNT

    SELECT c.name, COUNT(t._id) AS tickets FROM customers c LEFT JOIN tickets t ON t.customerId = c._id GROUP BY c.name ORDER BY tickets DESC, c.name;

    db.customers.aggregate([
      {
        $lookup: {
          from: "tickets",
          localField: "_id",
          foreignField: "customerId",
          as: "t"
        }
      },
      { $project: { _id: 0, name: 1, tickets: { $size: "$t" } } },
      { $sort: { tickets: -1, name: 1 } }
    ])
    • $lookup keeps every customer, even with no tickets: a LEFT JOIN by default.
    • Because the join result is an array, COUNT becomes $size and no $group is needed.
    Result of LEFT JOIN with COUNT on the sample data
    nametickets
    Priya Nair2
    Aarav Mehta1
    Daniel Okafor1
    Sofia Rossi1

    + 1 more rows

  • findtickets10

    IS NULL

    SELECT ticketNumber, subject FROM tickets WHERE assignee IS NULL ORDER BY ticketNumber;

    db.tickets.find({ assignee: null }, { _id: 0, ticketNumber: 1, subject: 1 })
      .sort({ ticketNumber: 1 })
    • IS NULL becomes { assignee: null }.
    • Unlike SQL, this also matches documents where assignee is missing; use { $type: "null" } for explicit nulls only.
    Result of IS NULL on the sample data
    ticketNumbersubject
    T-5004Cannot verify my email address
    T-5005Bulk order delivery delayed
  • findproducts11

    BETWEEN

    SELECT name, price FROM products WHERE price BETWEEN 1000 AND 15000 ORDER BY price;

    db.products.find({ price: { $gte: 1000, $lte: 15000 } }, { _id: 0, name: 1, price: 1 })
      .sort({ price: 1 })
    • BETWEEN is inclusive on both ends, so it maps to $gte and $lte together.
    Result of BETWEEN on the sample data
    nameprice
    Wireless mouse1299
    Mechanical keyboard6499
    Noise-cancelling headphones12999
  • aggregatecustomers12

    GROUP BY with COUNT(*)

    SELECT country, COUNT(*) AS customers FROM customers GROUP BY country ORDER BY customers DESC, country;

    db.customers.aggregate([
      { $group: { _id: "$country", customers: { $sum: 1 } } },
      { $sort: { customers: -1, _id: 1 } }
    ])
    • COUNT(*) is { $sum: 1 } inside $group.
    • The two ORDER BY keys become a two-key $sort.
    Result of GROUP BY with COUNT(*) on the sample data
    _idcustomers
    IN2
    IT1
    JP1
    NG1
  • findorders13

    LIMIT with OFFSET

    SELECT orderNumber, total FROM orders ORDER BY total DESC LIMIT 3 OFFSET 1;

    db.orders.find({}, { _id: 0, orderNumber: 1, total: 1 })
      .sort({ total: -1 })
      .skip(1)
      .limit(3)
    • OFFSET becomes skip(1) and LIMIT becomes limit(3).
    • For deep pages, prefer a range filter on the sort key over a large skip.
    Result of LIMIT with OFFSET on the sample data
    orderNumbertotal
    A-100532496
    A-100312999
    A-10019097

How the tool works

Four steps, and none of them touch your database.

  1. 1

    Paste a SELECT

    One statement at a time. Table names become collection names unless you say otherwise.

  2. 2

    Add a sample (optional)

    A sample document helps with nested fields and ObjectId references.

  3. 3

    Read the mapping

    Each SQL clause is matched to its MongoDB stage or operator, with differences called out.

  4. 4

    Copy the result

    mongosh, Node.js or PyMongo, ready to run.

What it won't do

Honest limits, so you know what to expect.

  • Convert INSERT, UPDATE, DELETE or DDL. Only SELECT statements are translated.
  • Migrate your schema. Normalised tables often belong embedded in one MongoDB document.
  • Run anything against your database.

The same guard as the product

Generated queries pass the check wuup runs on every connected database: only read stages, no server-side JavaScript, at most 30 stages.

Writes ($out, $merge)
Refused
Server JS ($where, $function)
Refused
Stages
Read-only allowlist
Your database
Never contacted
Inputs
Cached 7 days by hash

The concepts
behind the output

A short explanation of what the tool produces and the mistakes it is built to avoid.

How SQL maps to MongoDB

WHERE becomes the find filter or a $match stage. SELECT columns become a projection. ORDER BY, LIMIT and OFFSET become sort, limit and skip. GROUP BY becomes $group, with COUNT(*) as { $sum: 1 } and SUM, AVG, MIN and MAX as accumulators. HAVING is a $match after the $group. JOIN becomes $lookup.

Where the two behave differently

{ field: null } matches missing fields as well as nulls. Regex matching is case-sensitive by default. $lookup is a LEFT JOIN unless you $unwind the result. Fields can hold arrays, so an equality match on an array field checks whether the array contains the value.

These differences are the main source of wrong results when porting reports, so the converter calls them out in its explanation.

Keep going

Guides that go deeper, the product that runs these queries for you, and the other free tools.

Questions,
answered

Stop writing queries.
Start asking wuup.