Free tool

MongoDB query generator

Type what you want in plain English, paste one sample document, and get a MongoDB query you can run: a find() with filter, projection, sort and limit, or an aggregate() when grouping is needed. Every answer is checked so it can only read.

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

Describe the documents you want. Press ⌘ + Enter to run.

552/20,000

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

14 worked examples,
run against real data

Every example ran on the sample orders, customers, tickets, products, events 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

    Delivered orders, newest first

    “Show delivered orders, newest first”

    db.orders.find({ status: "delivered" })
      .sort({ createdAt: -1 })
      .limit(20)
    • An equality match on status keeps only delivered orders.
    • sort({ createdAt: -1 }) puts the most recent order first.
    Result of Delivered orders, newest first on the sample data
    orderNumbertotalpaymentMethod
    A-10019097upi
    A-100312999card
  • findorders02

    Large card payments

    “Orders over 10,000 paid by card, biggest first, just the number, status and total”

    db.orders.find({ total: { $gt: 10000 }, paymentMethod: "card" }, { _id: 0, orderNumber: 1, status: 1, total: 1 })
      .sort({ total: -1 })
      .limit(20)
    • Two conditions in one filter object are combined with AND.
    • $gt is a strict greater than, so an order of exactly 10,000 is excluded.
    Result of Large card payments on the sample data
    orderNumberstatustotal
    A-1002shipped37998
    A-1005pending32496
    A-1003delivered12999
  • findorders03

    Orders placed in one month

    “Orders placed in September 2026, oldest first”

    db.orders.find({
      createdAt: {
        $gte: ISODate("2026-09-01T00:00:00Z"),
        $lt: ISODate("2026-10-01T00:00:00Z")
      }
    })
      .sort({ createdAt: 1 })
      .limit(20)
    • A half-open range ($gte the first day, $lt the next month) catches every timestamp in September.
    • Dates are compared as real BSON dates, written ISODate(...) in mongosh.
    Result of Orders placed in one month on the sample data
    orderNumberstatus
    A-1001delivered
    A-1002shipped
    A-1004cancelled
  • findorders04

    Orders that contain a product

    “Which orders include the wireless mouse (SKU MS-110)?”

    db.orders.find({ "items.sku": "MS-110" }, { _id: 0, orderNumber: 1, status: 1 })
      .sort({ orderNumber: 1 })
      .limit(20)
    • Dot notation ("items.sku") looks inside every element of the items array.
    • A document matches if any line item has that SKU, no $unwind needed.
    Result of Orders that contain a product on the sample data
    orderNumberstatus
    A-1001delivered
    A-1004cancelled
  • findorders05

    Orders to two cities, not cancelled

    “Orders shipped to Mumbai or Bengaluru that were not cancelled”

    db.orders.find({
      "shipping.city": { $in: ["Mumbai", "Bengaluru"] },
      status: { $ne: "cancelled" }
    })
      .sort({ total: -1 })
      .limit(20)
    • $in matches any of the listed cities inside the embedded shipping object.
    • $ne excludes cancelled orders (it also matches documents with no status field).
    Result of Orders to two cities, not cancelled on the sample data
    orderNumbershipping.citytotal
    A-1002Mumbai37998
    A-1005Mumbai32496
    A-1001Bengaluru9097
  • findorders06

    Orders for one customer id

    “All orders for customer 64f1a0c2e4b0a1b2c3d4e502, newest first”

    db.orders.find({ customerId: ObjectId("64f1a0c2e4b0a1b2c3d4e502") })
      .sort({ createdAt: -1 })
      .limit(20)
    • customerId is stored as an ObjectId, so the value must be ObjectId(...), not a plain string.
    • A string would silently match nothing: the most common reason a correct-looking query returns no rows.
    Result of Orders for one customer id on the sample data
    orderNumberstatustotal
    A-1005pending32496
    A-1002shipped37998
  • findcustomers07

    Paid customers who opted in

    “Name, email and plan of customers on a paid plan who opted in to marketing”

    db.customers.find({ plan: { $in: ["pro", "business"] }, marketingOptIn: true }, { _id: 0, name: 1, email: 1, plan: 1 })
      .sort({ name: 1 })
      .limit(20)
    • "Paid plan" is read as pro or business, based on the plan values in the sample.
    • marketingOptIn is a boolean, so it is matched with true, not the string "true".
    Result of Paid customers who opted in on the sample data
    nameemailplan
    Aarav Mehtaaarav@example.compro
    Sofia Rossisofia@example.compro
  • findcustomers08

    Customers with no phone number

    “Customers who have no phone number on file”

    db.customers.find({ phone: { $exists: false } }, { _id: 0, name: 1, country: 1 })
      .sort({ name: 1 })
      .limit(20)
    • $exists: false matches documents where the field is missing entirely.
    • Use { phone: null } instead if you also want documents where phone is explicitly null.
    Result of Customers with no phone number on the sample data
    namecountry
    Daniel OkaforNG
    Kenji WatanabeJP
    Sofia RossiIT
  • findtickets09

    High-priority tickets still open

    “Open or pending tickets with high or urgent priority, oldest first”

    db.tickets.find({
      status: { $in: ["open", "pending"] },
      priority: { $in: ["high", "urgent"] }
    })
      .sort({ createdAt: 1 })
      .limit(20)
    • Two $in lists, combined with AND: the status must be in the first and the priority in the second.
    • Oldest first puts the tickets that have waited longest at the top.
    Result of High-priority tickets still open on the sample data
    ticketNumberprioritysubject
    T-5001highInvoice shows the wrong GST number
    T-5005urgentBulk order delivery delayed
  • findtickets10

    Tickets with no first response

    “Unresolved tickets that have not had a first response yet”

    db.tickets.find({ firstResponseAt: null, status: { $nin: ["resolved", "closed"] } })
      .sort({ createdAt: 1 })
      .limit(20)
    • { firstResponseAt: null } matches both a null value and a missing field.
    • $nin excludes tickets that are already resolved or closed.
    Result of Tickets with no first response on the sample data
    ticketNumberchannelsubject
    T-5005phoneBulk order delivery delayed
  • findproducts11

    Out of stock but still listed

    “Active products that are out of stock”

    db.products.find({ stock: 0, active: true }, { _id: 0, sku: 1, name: 1 })
      .limit(20)
    • stock: 0 is an exact match; use { $lte: 0 } if negative stock is possible in your data.
    • active: true leaves out products that are already unlisted.
    Result of Out of stock but still listed on the sample data
    skuname
    MS-110Wireless mouse
  • findproducts12

    Top-rated wireless products

    “Wireless products rated 4.5 or higher, cheapest first”

    db.products.find({ tags: "wireless", rating: { $gte: 4.5 } }, { _id: 0, name: 1, price: 1, rating: 1 })
      .sort({ price: 1 })
      .limit(20)
    • Matching an array field against a single value ({ tags: "wireless" }) checks whether the array contains it.
    • $gte includes products rated exactly 4.5.
    Result of Top-rated wireless products on the sample data
    namepricerating
    Mechanical keyboard64994.6
    Noise-cancelling headphones129994.7
  • findproducts13

    Name contains a word, any case

    “Products whose name contains "cable", ignoring case”

    db.products.find({ name: { $regex: "cable", $options: "i" } }, { _id: 0, sku: 1, name: 1, active: 1 })
      .limit(20)
    • $regex with the i option matches Cable, CABLE or cable anywhere in the name.
    • An unanchored, case-insensitive regex scans every document; anchor it (^USB) when you can.
    Result of Name contains a word, any case on the sample data
    skunameactive
    CB-050USB-C cable, 2 mfalse
  • findevents14

    Mobile page views, last 7 days

    “Page views from mobile devices in the last 7 days”

    db.events.find({
      event: "page_view",
      device: "mobile",
      $expr: {
        $gte: [
          "$ts",
          { $dateSubtract: { startDate: "$$NOW", unit: "day", amount: 7 } }
        ]
      }
    })
      .sort({ ts: -1 })
      .limit(20)
    • $$NOW is the server time when the query runs, so the window moves with the clock.
    • $dateSubtract inside $expr computes "7 days ago" without hard-coding a date.

    Result depends on when it runs ($$NOW or $sample), so no fixed preview.

How the tool works

Four steps, and none of them touch your database.

  1. 1

    Describe the result

    Say which documents you want and what to return, the way you'd ask a colleague.

  2. 2

    Add a sample document

    One document is enough to give the model your real field names and types. Your data is never queried.

  3. 3

    Get checked code

    The query is validated and passed through the same read-only guard wuup uses on live databases.

  4. 4

    Copy it anywhere

    Switch between mongosh, the Node.js driver and PyMongo. ObjectIds and dates are written correctly in each.

What it won't do

Honest limits, so you know what to expect.

  • Connect to your database or run anything. It only writes text.
  • Write, update or delete data. $out, $merge, $where and $function are refused.
  • Know fields you didn't show it. Missing context is listed under assumptions.
  • Store your sample for training. Inputs are cached for 7 days by a hash so repeat requests are instant.

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.

find() or aggregate()?

Most questions about specific documents are a find(): a filter to choose documents, an optional projection to choose fields, a sort and a limit. It is the fastest kind of query and the easiest to read.

As soon as you need totals, averages, counts per group, joins to another collection or computed fields, you need an aggregation pipeline. The generator picks the simpler form when it can and tells you why in the explanation bullets.

Why a sample document matters

MongoDB has no fixed schema, so a model guessing field names will often write a query that looks right and returns nothing. A single real document tells it that the date field is createdAt, that customerId is an ObjectId and that tags is an array.

Paste a document with sensitive values replaced. The tool refuses inputs that look like connection strings, private keys or API keys.

Common mistakes the generator avoids

Comparing an ObjectId field with a string, comparing dates with strings, using $ne where the field might be missing, and unanchored regexes on large collections. When one of these is a risk, it appears under warnings.

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.