Skip to main content
Once you have your models defined, querying your database becomes incredibly straightforward! Each Esix model acts as a powerful query builder, allowing you to fluently search and retrieve documents from your MongoDB collection.

Finding a Single Record

When you need to find a specific document by its ID, use the find 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:
To get the first model that matches your query conditions, use the first method:
You can also get all the models in the collection.
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

The where method supports comparison operators for numeric and date comparisons, similar to Laravel’s Eloquent:
Each call above returns an array of matching records. For example, adults looks like: You can chain multiple comparison operators together:
workingAgeUsers 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

Use whereNull 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. Use orWhere 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 use whereIn 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

Use distinct to get the unique values of a field across the current query:
The result is a deduplicated array of values for the field, respecting any active where constraints. Once your collection has a text index, use search 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 is paginate(page, perPage), which returns the page of models alongside the metadata you need to render pagination UIs:
For more control, you can fall back to manual offset pagination using 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 are average, count, max, min, percentile, and sum.
When the query matches no documents, the numeric aggregates (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.