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

http
GET /api/query-2jz/User

Get Single Record

Retrieve a specific record by ID

http
GET /api/query-2jz/User/1

Filtering

Filter records using the where parameter with various operators.

Basic Filters

Equality

http
GET /api/query-2jz/User?where={"name":"John Doe"}

Multiple Conditions

http
GET /api/query-2jz/User?where={"status":"active","age":{"$gte":18}}

Complex Filters

http
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

http
GET /api/query-2jz/User?orderBy=name:asc

Multiple Field Sort

http
GET /api/query-2jz/User?orderBy=status:asc,name:desc

Default Sort Direction

http
GET /api/query-2jz/User?orderBy=createdAt

Defaults to ascending order

Pagination

Control the number of results and implement pagination.

Pagination Parameters

Limit Results

http
GET /api/query-2jz/User?limit=10

Skip Results

http
GET /api/query-2jz/User?offset=20&limit=10

Page-based Pagination

http
GET /api/query-2jz/User?page=2&limit=10

Automatically calculates offset: (page - 1) * limit

Relationships

Load related data using the include parameter.

Include Related Data

Single Relationship

http
GET /api/query-2jz/Post?include=author

Multiple Relationships

http
GET /api/query-2jz/Post?include=author,comments

Nested Relationships

http
GET /api/query-2jz/Post?include=author,comments.author

Field Selection

Select only the fields you need using the select parameter.

Select Specific Fields

Select Fields

http
GET /api/query-2jz/User?select=id,name,email

Exclude Fields

http
GET /api/query-2jz/User?select=-password,-internalNotes

Mixed Selection

http
GET /api/query-2jz/User?select=id,name,email,-password

Advanced Queries

Combine multiple parameters for complex queries.

http
// 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.count

Query 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