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:
When you are working with multiple models, you can use methods like
where
which returns an instance of a QueryBuilder. The Query Builder can be used to
filter, sort, and limit your searches.
Comparison Operators
Thewhere method supports comparison operators for numeric and date comparisons, similar to Laravel’s Eloquent:
adults looks like:
You can chain multiple comparison operators together:
affordableProducts
popularPosts
Supported Operators
Note: The two-parameter syntax
where('status', 'published') is still supported for equality comparisons and remains the recommended approach for simple equality checks.
The values you pass to where, orWhere, whereIn, and whereNotIn are
type-checked against the model’s property types, so passing a string to a
numeric field like where('age', '>', '18') is caught at compile time. For
array fields, the element type is also accepted, so where('tags', 'news')
compiles when tags is a string[].
Null Checks
UsewhereNull to retrieve models where a field is null. Following
MongoDB’s null-equality semantics, this also matches documents where the field
is missing entirely:
Conversely,
whereNotNull retrieves models where a field is present and not
null. Documents where the field is null or missing are excluded:
Or Conditions
By default, chained conditions are combined with a logical AND. UseorWhere
to combine conditions with a logical OR instead. It accepts the same arguments
as where, including comparison operators, so you can express selections like
“null or below a version” without loading the whole collection:
AND binds tighter than OR, just like in SQL. Any
where calls following an
orWhere are ANDed into the most recent OR group, so
where(a).orWhere(b).where(c) selects documents matching a OR (b AND c):
orWhere is only available on the Query Builder, so start your chain with
where, whereNull, or another query method. Note that orWhere cannot be
combined with search().
Array Queries
You can usewhereIn to retrieve models where a column’s value is within a
given array:
Conversely, you can use
whereNotIn to retrieve models where a column’s value
is not within a given array:
If you are only interested in a single attribute of a model, you can use the
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:
If the collection has no text index, Esix surfaces a descriptive error
explaining how to create one.
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:
You can find out more about the different methods available by consulting the
Esix source on GitHub.
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.