Query
Query
These methods allow you to refine entry queries by applying conditions, filters, and relational data. You can filter entries based on specific field values, include referenced entries, and limit the number of results.
Example:
const query = stack.contentType("contentTypeUid").Entry().query();addParams
The addParam method adds a query parameter to the query.
| Name | Type | Description |
|---|---|---|
| paramObj (required) | string | Add key-value pairs |
Example:
import { BaseEntry, FindEntry } from '@contentstack/delivery-sdk'
interface BlogPostEntry extends BaseEntry {
// custom entry types
}
const query = stack.contentType(contentType_uid).entry().query();
const result = await query
.addParams({"key": "value"})
.find<BlogPostEntry>();addQuery
The addQuery method adds multiple query parameters to the query.
| Name | Type | Description |
|---|---|---|
| key (required) | string | Add filter query key |
| value (required) | string | number | Add the corresponding value to the filter query key |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query
.addQuery("query_param_key", "query_param_value")
.find<BlogPostEntry>();find
The find method retrieves the details of the specified entry.
Example:
const result = await stack
.contentType("contentType1Uid")
.entry()
.query()
.find<BlogPostEntry>();includeCount
The includeCount method retrieves count and data of objects in the result.
Example:
const query = stack.contentType(contentType_uid).entry().query(); const result = await query.includeCount().find<BlogPostEntry>();
orderByAscending
The orderByAscending method sorts the results in ascending order based on the specified field UID.
| Name | Type | Description |
|---|---|---|
| key (required) | string | Field UID to sort the results |
Example:
const query = stack.contentType(contentType_uid).entry().query();
const result = await query
.orderByAscending()
.find<BlogPostEntry>();orderByDescending
The orderByDescending method sorts the results in descending order based on the specified key.
| Name | Type | Description |
|---|---|---|
| key (required) | string | Field UID to sort the results |
Example:
const query = stack.contentType(contentType_uid).entry().query();
const result = await query
.orderByDescending()
.find<BlogPostEntry>();param
The param method adds query parameters to the URL.
| Name | Type | Description |
|---|---|---|
| key (required) | string | Add any param to include in the response |
| value (required) | string | number | Add the corresponding value of the param key |
Example:
const query = stack.contentType(contentType_uid).entry().query();
const result = await query
.param("key", "value")
.find<BlogPostEntry>();queryOperator
The queryOperator method retrieves the entries as per the given operator.
| Name | Type | Description |
|---|---|---|
| queryType (required) | QueryOperatorEnum | Type of query operator to apply |
| queryObjects (required) | Query[] | Query instances to apply the query to |
Example:
import contentstack, { QueryOperation, QueryOperator } from '@contentstack/delivery-sdk';
const stack = contentstack.stack('apiKey', 'deliveryToken', 'environment');
// Create main query
const query = stack
.contentType('contentType1Uid')
.entry()
.query();
// Create subquery 1
const subQuery1 = stack
.contentType('contentType2Uid')
.entry()
.query()
.where('price', QueryOperation.IS_LESS_THAN, 90);
// Create subquery 2
const subQuery2 = stack
.contentType('contentType3Uid')
.entry()
.query()
.where('discount', QueryOperation.INCLUDES, [20, 45]);
// Apply the query operator (AND/OR)
query.queryOperator(QueryOperator.AND, subQuery1, subQuery2);
// Execute the query
const result = await query.find();
console.log('Result:', result);removeParam
The removeParam method removes a query parameter from the query.
| Name | Type | Description |
|---|---|---|
| key (required) | string | Specify the param key you want to remove |
Example:
const query = stack.contentType(contentType_uid).entry().query();
const result = await query
.removeParam("query_param_key")
.find<BlogPostEntry>();where
The where method filters the results based on the specified criteria.
| Name | Type | Description |
|---|---|---|
| fieldUid (required) | string | Specify the field the comparison is made from |
| queryOperation (required) | QueryOperationEnum | Specify the comparison criteria |
| fields (required) | string | strings[] | Specify the field the comparison is made to |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query
.where(
"field_UID",
QueryOperation.IS_LESS_THAN,
["field1", "field2"])
.find<BlogPostEntry>();whereIn
The whereIn method retrieves the entries that meet the query conditions made on referenced fields.
| Name | Type | Description |
|---|---|---|
| referenceUid (required) | string | UID of the reference field to query |
| queryInstance (required) | Query | Query instance to include in the where clause |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
query.whereIn("brand");
const result = await query.find<BlogPostEntry>();whereNotIn
The whereNotIn method retrieves the entries that do not meet the query conditions made on referenced fields.
| Name | Type | Description |
|---|---|---|
| referenceUid (required) | string | UID of the reference field to query |
| queryInstance (required) | Query | Query instance to include in the where clause |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
query.whereNotIn("brand");
const result = await query.find<BlogPostEntry>();skip
The skip method will skip a specific number of entries in the output.
| Name | Type | Description |
|---|---|---|
| skipBy (required) | int | Enter the number of entries to be skipped. |
Example:
const result = await stack
.contentType(contentType_uid)
.entry()
.query()
.skip(5)
.find<BlogEntry>();limit
The limit method will return a specific number of entries in the output.
| Name | Type | Description |
|---|---|---|
| limit (required) | int | Enter the maximum number of entries to be returned. |
Example:
const result = await stack
.contentType(contentType_uid)
.entry()
.query()
.limit(5)
.find<BlogEntry>();or
The or method retrieves the entries that meet either of the conditions specified.
| Name | Type | Description |
|---|---|---|
| queries (required) | array | Array of query objects or raw queries |
Example:
const query1 = stack.contentType('contenttype_uid').entry().query().containedIn('fieldUID', ['value']);
const query2 = stack.contentType('contenttype_uid').entry().query().where('fieldUID', QueryOperation.EQUALS, 'value2');
const query = await stack.contentType('contenttype_uid').entry().query().or(query1, query2).find<BlogPostEntry>();and
The and method retrieves the entries that meet all the specified conditions.
| Name | Type | Description |
|---|---|---|
| queries (required) | array | Array of query objects or raw queries |
Example:
const query1 = stack.contentType('contenttype_uid').entry().query().containedIn('fieldUID', ['value']);
const query2 = stack.contentType('contenttype_uid').entry().query().where('fieldUID', QueryOperation.EQUALS, 'value2');
const query = await stack.contentType('contenttype_uid').entry().query().and(query1, query2).find<BlogPostEntry>();containedIn
The containedIn method retrieves the entries that contain the conditions specified.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
| value (required) | array | Array of values that are to be used to match or compare |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query.containedIn('fieldUid', ['value1', 'value2']).find();notContainedIn
The notContainedIn method retrieves the entries where the specified conditions are absent.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
| value (required) | array | Array of values that are to be used to match or compare |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query.notContainedIn('fieldUid', ['value1', 'value2']).find();equalTo
The equalTo method retrieves entries that match the specified conditions exactly.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
| value (required) | array | Array of values that are to be used to match or compare |
Example:
const query = await stack.contentType('contenttype_uid').entry().query().equalTo('fieldUid', 'value').find<BlogPostEntry>();exists
The exists method retrieves the entries that satisfy the specified condition of existence.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query.exists('fieldUid').find();notExists
The notExists method retrieves entries where the specified conditions are not met.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query.notExists('fieldUid').find();getQuery
The getQuery method retrieves the entries as per the specified query.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query.query({'brand': {'$nin_query': {'title': 'Apple Inc.'}}}).getQuery();
// OR
const asset = await stack.asset().query({'brand': {'$nin_query': {'title': 'Apple Inc.'}}}).getQuery();greaterThan
The greaterThan method retrieves the entries that are greater than the specified condition.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
| value (required) | array | Array of values that are to be used to match or compare |
Example:
const query = stack.contentType('contenttype_uid').query().where('title', QueryOperation.EQUALS, 'value');
const entryQuery = await stack.contentType('contenttype_uid').query().greaterThan('fieldUid', 'value').find<BlogPostEntry>();greaterThanOrEqualTo
The greaterThanOrEqualTo method retrieves entries that meet the specified condition of being greater than or equal to a certain value.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
| value (required) | array | Array of values that are to be used to match or compare |
Example:
const query = stack.contentType('contenttype_uid').query().where('title', QueryOperation.EQUALS, 'value');
const entryQuery = await stack.contentType('contenttype_uid').query().greaterThanOrEqualTo('fieldUid', 'value').find<BlogPostEntry>();lessThan
The lessThan method retrieves the entries that are less than the specified condition.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
| value (required) | array | Array of values that are to be used to match or compare |
Example:
const query = stack.contentType('contenttype_uid').query().where('title', QueryOperation.EQUALS, 'value');
const entryQuery = await stack.contentType('contenttype_uid').query().lessThan('fieldUid', 'value').find<BlogPostEntry>();lessThanOrEqualTo
The lessThanOrEqualTo method retrieves entries that meet the specified condition of being less than or equal to a certain value.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
| value (required) | array | Array of values that are to be used to match or compare |
Example:
const query = stack.contentType('contenttype_uid').query().where('title', QueryOperation.EQUALS, 'value');
const entryQuery = await stack.contentType('contenttype_uid').query().lessThanOrEqualTo('fieldUid', 'value').find<BlogPostEntry>();referenceIn
The referenceIn method retrieves the entries that are referenced.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the reference field |
| value (required) | object | RAW (JSON) queries |
Example:
const query = stack.contentType('contenttype_uid').query().where('title', QueryOperation.EQUALS, 'value');
const entryQuery = await stack.contentType('contenttype_uid').query().referenceIn('reference_uid', query).find<BlogPostEntry>();referenceNotIn
The referenceNotIn method retrieves the entries where the referenced items are not included.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the reference field |
| value (required) | object | RAW (JSON) queries |
Example:
const query = stack.contentType('contenttype_uid').query().where('title', QueryOperation.EQUALS, 'value');
const entryQuery = await stack.contentType('contenttype_uid').query().referenceNotIn('reference_uid', query).find<BlogPostEntry>();regex
The regex method retrieves entries that match a specified regular expression pattern.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
| value (required) | array | Array of values that are to be used to match or compare |
| options (required) | any | Match or compare value in entry |
Example:
const query = stack.contentType("contentTypeUid").entry().query();
const result = await query.regex('title','^Demo').find();
// OR
const result = await query.regex('title','^Demo', 'i').find<BlogPostEntry>();search
The search method retrieves the entries that match the specified search criteria.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the field |
Example:
const entryQuery = await stack.contentType('contenttype_uid').query().search('key').find<BlogPostEntry>();tags
The tags method fetches the entries that are associated with specific tags.
| Name | Type | Description |
|---|---|---|
| value (required) | array | Array of tags |
Example:
const query = stack.contentType('contenttype_uid').query().where('title', QueryOperation.EQUALS, 'value');
const entryQuery = await stack.contentType('contenttype_uid').query().tags(['tag1']).find<BlogPostEntry>();