What query operators are available in MongoDB?
MongoDB queries are themselves JSON objects, so instead of an SQL WHERE clause you describe the match with special $-prefixed operator keys.
A query like { age: 18 } means "exactly 18". To express anything richer - greater than, not equal, ranges - you nest an operator object as the field's value. The comparison operators cover the obvious tests:
| Operator | Meaning |
|---|---|
$eq |
Equal to |
$ne |
Not equal to |
$gt |
Greater than |
$gte |
Greater than or equal |
$lt |
Less than |
$lte |
Less than or equal |
$and |
Logical AND |
$or |
Logical OR |
So { day: { $gt: new Date('2000-01-01') } }` reads as "documents whose `day` is later than 2000-01-01". The logical operators `$and / $or take an array of sub-conditions and let you combine several of these tests.
**Why the $` prefix?** It is how MongoDB tells a field name apart from a command. Anything starting with `$ is interpreted as an operator, so you can never accidentally match a literal field called gt. Memory tip: the names are just the math comparisons spelled out - $gt` = greater than, `$lte = less than or equal - so once you know the abbreviation pattern you know them all.
Go deeper:
Query and Projection Operators (MongoDB Manual) — the complete reference for every
$`-operator, with the comparison (`$eq/$gt`/`$lt...) and logical ($and`/`$or) groups laid out.