Finding a Single Record
When you need to find a specific document by its ID, use thefind method:
find accepts both ObjectId hex strings and plain string ids. If the value is
not a valid 24-character hex string, Esix transparently falls back to a string
_id lookup. Invalid input never throws — find just returns null when no
matching document exists.
You can also find a model by a specific field using the findBy method:
first
method:
| id | name |
|---|---|
5f5a474b32fa462a5724ff7d | AA 100 → JFK |
5f5a474b32fa462a5724ff7e | BA 286 → LHR |
5f5a474b32fa462a5724ff7f | UA 245 → SFO |
where
which returns an instance of a QueryBuilder. The Query Builder can be used to
filter, sort, and limit your searches.
| id | title | status | categoryId | publishedAt |
|---|---|---|---|---|
6011a52b9f1b2c4d8e7f3a21 | Introducing Aggregate Functions | published | 4 | 1736380800000 |
60119e8a9f1b2c4d8e7f3a14 | Querying With Comparison Operators | published | 4 | 1735862400000 |
601198119f1b2c4d8e7f3a09 | Pagination Patterns in Esix | published | 4 | 1734998400000 |
Comparison Operators
Thewhere method supports comparison operators for numeric and date comparisons, similar to Laravel’s Eloquent:
adults looks like:
| id | name | age | status |
|---|---|---|---|
5f5a474b32fa462a5724ff7d | Alice | 32 | active |
5f5a474b32fa462a5724ff7e | Bob | 45 | active |
5f5a474b32fa462a5724ff7f | Carol | 28 | active |
| id | name | age | status |
|---|---|---|---|
5f5a474b32fa462a5724ff7d | Alice | 32 | active |
5f5a474b32fa462a5724ff7e | Bob | 45 | active |
5f5a474b32fa462a5724ff80 | Dimitri | 64 | active |
| id | name | price | inStock |
|---|---|---|---|
60119e8a9f1b2c4d8e7f3a14 | Desk Lamp | 24.99 | true |
60119e8a9f1b2c4d8e7f3a15 | Floor Lamp | 79.00 | true |
60119e8a9f1b2c4d8e7f3a16 | Reading Lamp | 39.50 | true |
| id | title | views | status |
|---|---|---|---|
6011a52b9f1b2c4d8e7f3a21 | Introducing Aggregate Functions | 12_840 | published |
60119e8a9f1b2c4d8e7f3a14 | Comparison Operators Are Here | 6_120 | published |
601198119f1b2c4d8e7f3a09 | Pagination Patterns in Esix | 3_402 | published |
Supported Operators
| Operator | Description | Example |
|---|---|---|
= | Equals | .where('age', '=', 25) |
!= | Not equals | .where('status', '!=', 'banned') |
<> | Not equals (alternate) | .where('status', '<>', 'banned') |
> | Greater than | .where('age', '>', 18) |
>= | Greater than or equal | .where('score', '>=', 100) |
< | Less than | .where('age', '<', 65) |
<= | Less than or equal | .where('price', '<=', 50) |
where('status', 'published') is still supported for equality comparisons and remains the recommended approach for simple equality checks.
Array Queries
You can usewhereIn to retrieve models where a column’s value is within a
given array:
| id | name | age | status |
|---|---|---|---|
1 | Alice | 32 | active |
2 | Bob | 45 | active |
3 | Carol | 28 | inactive |
whereNotIn to retrieve models where a column’s value
is not within a given array:
| id | name | age | status |
|---|---|---|---|
4 | Dimitri | 64 | active |
5 | Eli | 19 | active |
6 | Farah | 37 | active |
pluck method to get an array of values for that attribute.
Distinct Values
Usedistinct to get the unique values of a field across the current query:
where constraints.
Full-Text Search
Once your collection has a text index, usesearch to run full-text queries:
| id | title | published | tag |
|---|---|---|---|
6011a52b9f1b2c4d8e7f3a21 | Querying MongoDB With TypeScript | true | typescript |
60119e8a9f1b2c4d8e7f3a14 | Why MongoDB Aggregations Matter | true | mongodb |
601198119f1b2c4d8e7f3a09 | Typed Schemas for MongoDB | true | mongodb |
Pagination
The fastest way to paginate ispaginate(page, perPage), which returns the
page of models alongside the metadata you need to render pagination UIs:
skip
and limit:
| id | name | category | price |
|---|---|---|---|
60119e8a9f1b2c4d8e7f3a21 | Bluetooth Speaker | electronics | 59.00 |
60119e8a9f1b2c4d8e7f3a22 | Smart Bulb | electronics | 14.99 |
60119e8a9f1b2c4d8e7f3a23 | USB-C Hub | electronics | 34.50 |
Aggregate Functions
Once you are happy with your query, you can use the aggregate functions available in Esix to perform calculations on the data set. The supported aggregates areaverage, count, max, min, percentile, and sum.
average,
max, min, percentile, sum) return 0 rather than throwing.
percentile requires n to be a finite number between 0 and 100. Any
other value (including NaN and Infinity) throws a descriptive error so
that bad inputs do not silently return misleading results.