Queries
Learn how to query your data with Query-2jz's powerful and intuitive query system.
What are Queries?
Queries in Query-2jz allow you to retrieve data from your models with filtering, sorting, pagination, and relationship loading. All queries are automatically optimized and cached.
Basic Queries
Start with simple queries to retrieve data from your models.
Get All Records
Retrieve all records from a model
GET /api/query-2jz/UserGet Single Record
Retrieve a specific record by ID
GET /api/query-2jz/User/1Filtering
Filter records using the where parameter with various operators.
Basic Filters
Equality
GET /api/query-2jz/User?where={"name":"John Doe"}Multiple Conditions
GET /api/query-2jz/User?where={"status":"active","age":{"$gte":18}}Complex Filters
GET /api/query-2jz/User?where={
"$and": [
{"status": "active"},
{"$or": [
{"age": {"$gte": 18}},
{"role": "admin"}
]}
]
}Sorting
Sort results using the orderBy parameter.
Sort Examples
Single Field Sort
GET /api/query-2jz/User?orderBy=name:ascMultiple Field Sort
GET /api/query-2jz/User?orderBy=status:asc,name:descDefault Sort Direction
GET /api/query-2jz/User?orderBy=createdAtDefaults to ascending order
Pagination
Control the number of results and implement pagination.
Pagination Parameters
Limit Results
GET /api/query-2jz/User?limit=10Skip Results
GET /api/query-2jz/User?offset=20&limit=10Page-based Pagination
GET /api/query-2jz/User?page=2&limit=10Automatically calculates offset: (page - 1) * limit
Relationships
Load related data using the include parameter.
Include Related Data
Single Relationship
GET /api/query-2jz/Post?include=authorMultiple Relationships
GET /api/query-2jz/Post?include=author,commentsNested Relationships
GET /api/query-2jz/Post?include=author,comments.authorField Selection
Select only the fields you need using the select parameter.
Select Specific Fields
Select Fields
GET /api/query-2jz/User?select=id,name,emailExclude Fields
GET /api/query-2jz/User?select=-password,-internalNotesMixed Selection
GET /api/query-2jz/User?select=id,name,email,-passwordAdvanced Queries
Combine multiple parameters for complex queries.
// Complex query example
GET /api/query-2jz/Post?where={
"status": "published",
"publishedAt": {"$gte": "2024-01-01"},
"author": {"role": "admin"}
}&orderBy=publishedAt:desc&limit=10&offset=0&include=author,comments&select=id,title,excerpt,author.name,comments.countQuery Optimization
Query-2jz automatically optimizes your queries for best performance.
Automatic Optimizations
- • Query batching for N+1 prevention
- • Index-aware query planning
- • Relationship loading optimization
- • Cache-friendly query patterns
- • Database-specific optimizations
- • Memory usage optimization
Performance Tips
- • Use indexes for filtered fields
- • Limit relationship depth
- • Select only needed fields
- • Use pagination for large datasets
- • Cache frequently accessed data
- • Monitor query performance