Engineering · 2 min read
MongoDB Operators & Vocabulary: SQL Intent to Documents
Translating SQL intent into MongoDB: comparison, $in, $regex, and $exists operators, a SQL→Mongo mapping table, and the relational→document vocabulary (table = collection, row = document).
Mongo speaks documents, so what you’re translating is the operation’s intent, not SQL syntax.
{ field: {$op: value} }
- Equal
$eq: the default, no operator needed.{field: <cond>}- Others:
{ age: {$gt: 30} }{ age: { $gt: 20, $lt: 40 } }// 20 < age < 40
Common operators
- Comparison
$gt/$gte/$lt/$lte$eq/$ne$in/$nin: matches any / matches none in a list, like SQL’s IN{ type: { $in: ["a","b","c"] } }
- String
$regex: like SQL’s LIKE{ name: { $regex: "abc", $options: "i" } }(i= case-insensitive)
- Existence
$exists: whether this field is present at all- In Mongo, Absent ≠ null:
username: nullmeans “the field is there, explicitly with no value”; Absent means “the document doesn’t have this key at all”
- In Mongo, Absent ≠ null:
- Logical —
$or/$and/$nortake an array{ $or: [ { age: { $lt: 18 } }, { vip: true } ] }
SQL → MongoDB
| SQL | MongoDB |
|---|---|
SELECT col | { $project: {...} } (or the find projection parameter) |
GROUP BY | { $group: { _id: "$field", ... } } |
ORDER BY | { $sort: {...} } |
IN (...) | { field: { $in: [a, b, c] } } |
IS NULL | { field: null } or { field: { $exists: false } } |
LIMIT | { $limit: n } |
Vocabulary: relational → MongoDB
| Relational (Postgres) | MongoDB | Notes |
|---|---|---|
| Table | Collection | a group of documents |
| Row | Document | a JSON/BSON object |
| Column | Field | but no fixed schema — fields can differ document to document |
| Schema | (not enforced) | schema is flexible, varies by document |
| Primary key | _id | auto-added, unique |
References
- MongoDB Query Operators — https://www.mongodb.com/docs/manual/reference/operator/query/
Related: back to the Cross-Engine DB Query overview; see how LIKE / ILIKE substring search maps to $regex, and how it all ties to the portable SQL core.