DevTools Logo
All posts

Build MongoDB Queries Without Memorizing Operators

August 15, 2026 · DevTools

mongodb
query-builder
aggregate
nosql
developer-tools

You need active users aged 18–65, sorted by name, limited to 100 — or a headcount by department from the same filter. MongoDB expresses both as JSON: a find document or an aggregate pipeline. The operators are consistent once you know the pattern, but $gte versus shorthand equality and nested $and arrays trip people up when typing by hand.

The MongoDB Query Builder turns field/operator/value rows into copy-ready JSON or db.collection.find() shell syntax. Everything runs in your browser — no Atlas connection, no Compass required for drafting.

Find vs aggregate

Find answers “give me documents matching these conditions”:

{
  "filter": { "status": "active", "age": { "$gte": 18 } },
  "projection": { "name": 1, "email": 1 },
  "sort": { "name": 1 },
  "limit": 100
}

Aggregate answers “transform matched documents through stages”:

[
  { "$match": { "status": "active" } },
  { "$group": { "_id": "$department", "count": { "$sum": 1 } } },
  { "$sort": { "count": -1 } },
  { "$limit": 10 }
]

Use find when you need rows back. Use aggregate when you need grouping, counting, or multi-stage reshaping.

Common operators

OperatorMeaningExample
$eqEqual (shorthand { field: value }){ "status": "active" }
$neNot equal{ "status": { "$ne": "deleted" } }
$gt / $gteGreater (or equal){ "age": { "$gte": 18 } }
$inValue in list{ "role": { "$in": ["admin", "editor"] } }
$regexPattern match{ "email": { "$regex": "@example.com", "$options": "i" } }
$existsField present or missing{ "email": { "$exists": true } }

Set the value type to number for numeric comparisons — MongoDB compares BSON types strictly.

AND / OR logic

Multiple filter rows combine left-to-right:

  • Two AND rows → { "$and": [ { ... }, { ... } ] }
  • Second row OR → { "$or": [ { ... }, { ... } ] }

For complex precedence you still need manual $and/$or nesting in code; the builder covers the common linear chains.

Shell output

Toggle Shell to get:

db.users.find({
  "status": "active"
}).sort({"name":1}).limit(100)

Paste into mongosh or adapt for Node.js driver calls.

Next steps

Try every example above in the MongoDB Query Builder — free, no signup, client-side only.