Learn how to retrieve models from the database using Esix query methods
Esix provides several intuitive methods for retrieving models from your MongoDB database. These methods are chainable and type-safe, making it easy to fetch the data you need.
Most query builder methods return a QueryBuilder instance, allowing you to chain multiple conditions. Call get() to execute the query and retrieve the results:
Here’s a practical example showing different ways to retrieve models:
class User extends BaseModel { public name = ''; public email = ''; public status = 'active';}// Get all usersconst allUsers = await User.all();// Find a specific user by IDconst user = await User.find('5f5a41cc3eb990709eafda43');if (user) { console.log(`Found user: ${user.name}`);}// Find user by emailconst userByEmail = await User.findBy('email', '[email protected]');// Get first active userconst firstActiveUser = await User.where('status', 'active').first();// Get all active usersconst activeUsers = await User.where('status', 'active').get();