Query
Query
An initializer is responsible for creating Query object. Provides support for all search queries
addParam
import Contentstack from 'contentstack'
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().addParam('include_count', 'true').toJSON().find()notEqualTo
| Name | Type | Description |
|---|---|---|
| key | string | UID of the field |
| value | any | Value used to match or compare |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().notEqualTo('title', 'some random title').toJSON().find();containedIn
| Name | Type | Description |
|---|---|---|
| key | string | UID of the field |
| value | array | Array of values that are to be used to match or compare |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().containedIn('title', ['Demo', 'Welcome']).toJSON().find();notContainedIn
| Name | Type | Description |
|---|---|---|
| key | string | UID of the field |
| value | array | Array of values that are to be used to match or compare |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().notContainedIn('title', ['Demo', 'Welcome']).toJSON().find();and
| Name | Type | Description |
|---|---|---|
| queries | array | array of Query objects or raw queries |
and with Query instances:
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'demo');
const Query2 = Stack.ContentType('content_type_1').Query().lessThan('age', 30);
const result = await Stack.ContentType('content_type_uid').Query().and(Query1, Query2).toJSON().find();and with raw queries:
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'demo').getQuery();
const Query2 = Stack.ContentType('content_type_1').Query().lessThan('age', 30).getQuery();
const result = await Stack.ContentType('content_type_uid').Query().and(Query1, Query2).toJSON().find();or
| Name | Type | Description |
|---|---|---|
| queries | array | array of Query objects or raw queries |
or with Query instances:
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'demo');
const Query2 = Stack.ContentType('content_type_1').Query().lessThan('age', 30);
const result = await Stack.ContentType('content_type_uid').Query().or(Query1, Query2).toJSON().find();or with raw queries:
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'demo').getQuery();
const Query2 = Stack.ContentType('content_type_1').Query().lessThan('age', 30).getQuery();
const result = await Stack.ContentType('content_type_uid').Query().or(Query1, Query2).toJSON().find();lessThan
| Name | Type | Description |
|---|---|---|
| key | string | UID of the field |
| value | any | Value used to match or compare |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().lessThan('age', 20).toJSON().find();lessThanOrEqualTo
| Name | Type | Description |
|---|---|---|
| key | string | UID of the field |
| value | any | Value used to match or compare |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().lessThanOrEqualTo('age', 20).toJSON().find();greaterThanOrEqualTo
| Name | Type | Description |
|---|---|---|
| key | string | UID of the field |
| value | any | Value used to match or compare |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().greaterThanOrEqualTo('age', 20).toJSON().find();limit
| Name | Type | Description |
|---|---|---|
| limit | number | maximum number of entries to be returned |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().limit(20).toJSON().find();ascending
| Name | Type | Description |
|---|---|---|
| key | string | field uid based on which the ordering will be done |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().ascending('field_uid').toJSON().find();descending
| Name | Type | Description |
|---|---|---|
| key | string | field uid based on which the ordering will be done |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().descending('field_uid').toJSON().find();exists
| Name | Type | Description |
|---|---|---|
| key | string | UID of the field |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().exists('field_uid').toJSON().find();notExists
| Name | Type | Description |
|---|---|---|
| key | string | UID of the field |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().notExists('field_uid').toJSON().find();only
| Name | Type | Description |
|---|---|---|
| values | string|array |
The only function with field_uid will include the data of only the specified fields for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().only('title').toJSON().find();The only function with an array of field_uids will include multiple fields for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().only(['title', 'description']).toJSON().find();In only, we have the only with a reference parameter, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter to include the data of only the specified field_uid for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').only('reference_field_uid','title').toJSON().find();In only, we have the only with a reference parameter with an array, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter with an array of fields to include the data of only the specified array of field_uids for each entry and exclude the data of all other fields.
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').only('reference_field_uid',['title', 'description']).toJSON().find();except
| Name | Type | Description |
|---|---|---|
| values | string|array |
The except function with field_uid will exclude the data of only the specified fields for each entry and includes the data of all other fields.
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().except('title').toJSON().find();The except function with an array of field_uids will except multiple fields for each entry and include the data of all other fields.
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().except(['title', 'description']).toJSON().find();In except, we have the only with a reference parameter, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter to except the data of only the specified field_uid for each entry and include the data of all other fields.
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').except('reference_field_uid','title').toJSON().find();In except, we have the only with a reference parameter with an array, where you need to enter the UID of the reference field in place of "reference_field_uid", and the second parameter with an array of fields to except the data of only the specified array of field_uids for each entry and include the data of all other fields.
import Contentstack from &'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').except('reference_field_uid',['title', 'description']).toJSON().find();regex
| Name | Type | Description |
|---|---|---|
| key | string | uid of the field |
| value | any | value used to match or compare |
| options | any | match or compare value in entry |
regex without options
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().regex('title', '^Demo').toJSON().find();regex with options
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().regex('title', '^Demo', 'i').toJSON().find();tags
| Name | Type | Description |
|---|---|---|
| values | array | array of tags |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().tags(['technology', 'business']).toJSON().find();includeReference
Fetches the entire content of referenced entry.
| Name | Type | Description |
|---|---|---|
| value | string|array |
Include Reference with reference_field_uids as array:
import Contentstack from 'contentstack'
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeReference(['reference_field_uid','other_reference_field_uid']).toJSON().find();Include Reference with reference_field_uids and its children reference
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeReference(['reference_field_uid', 'reference_field_uid.child_reference_field_uid']).toJSON().find();Include Reference with reference_field_uid:
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeReference('reference_field_uid').toJSON().find();referenceIn
| Name | Type | Description |
|---|---|---|
| fieldUid | string | Reference field Uid |
| query | object | RAW (JSON) queries |
referenceIn with Query instances
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'Demo');
const result = await Stack.ContentType('content_type_uid').Query().referenceIn('brand', Query1).toJSON().find();referenceIn with raw queries:
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().referenceIn('brand', {'title': 'Demo'}).toJSON().find();referenceNotIn
| Name | Type | Description |
|---|---|---|
| fieldUid | string | Reference field Uid |
| query | object | RAW (JSON) queries |
referenceNotIn with Query instances:
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const Query1 = Stack.ContentType('content_type_1').Query().where('title', 'Demo');
const result = await Stack.ContentType('content_type_uid').Query().referenceNotIn('brand', Query1).toJSON().find();referenceNotIn with raw queries:
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().referenceNotIn('brand', {'title': 'Demo'}).toJSON().find();includeReferenceContentTypeUID
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeReferenceContentTypeUID().toJSON().find();includeCount
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeCount().toJSON().find();includeContentType
import Contentstack from 'contentstack'
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeContentType().toJSON().find();includeBranch
import Contentstack from 'contentstack'
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeBranch().toJSON().find();includeEmbeddedItems
import Contentstack from 'contentstack'
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().includeEmbeddedItems().toJSON().find();includeFallback
import Contentstack from 'contentstack'
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
Stack.ContentType('content_type_uid;).Query().includeFallback().fetch()query
| Name | Type | Description |
|---|---|---|
| query | object | RAW (JSON) queries |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().query({'brand': {'$nin_query': {'title': 'Apple Inc.'}}}).toJSON().find();addQuery
Adds query to Entry object
| Name | Type | Description |
|---|---|---|
| key | string | Key of the query |
| value | any | Value of the query |
import Contentstack from 'contentstack'
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().addQuery('include_content_type', true).toJSON().find();getQuery
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const rawQuery = await Stack.ContentType('content_type_uid').Query().where('title', 'Demo').getQuery();language
| Name | Type | Description |
|---|---|---|
| language_code | string | language code. e.g. 'en-us', 'ja-jp', etc. |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().language('language_code').toJSON().find();find
| Name | Type | Description |
|---|---|---|
| param | object | Fetch options to fetch particular query |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().toJSON().find();findOne
| Name | Type | Description |
|---|---|---|
| param | object | Fetch options to fetch particular query |
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().toJSON().findOne();count
import Contentstack from 'contentstack';
const Stack = Contentstack.Stack({'api_key': 'api_key', 'delivery_token': 'delivery_token', 'environment': 'environment'});
const result = await Stack.ContentType('content_type_uid').Query().count().toJSON().find();equalAndBelow
The equalAndBelow method retrieves all entries for a specific taxonomy that match a specific term and all its descendant terms, requiring only the target term.
NoteThis query is applicable for the stack.Taxonomies() and stack.ContentType('uid').Query() methods.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the taxonomy field, specified as taxonomies.<taxonomy_uid> |
| value (required) | string | UID of the term to match or compare |
| levels | int | Depth level till which terms will be matched |
Example:
let data;
stack
.ContentType('ct_uid')
.Query()
.equalAndBelow("taxonomies.taxonomy_uid", "term_uid", 3)
.toJSON()
.find()
.then(response => {
// the response
data = response;
})below
The below method retrieves all entries for a specific taxonomy that match all of their descendant terms by specifying only the target term and a specific level.
NoteIf you don't specify the level, the default behavior is to retrieve terms up to level 10.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the taxonomy field, specified as taxonomies.<taxonomy_uid> |
| value (required) | string | UID of the term to match or compare |
| levels | int | Depth level till which terms will be matched |
Example:
let data;
stack
.ContentType('ct_uid')
.Query()
.below("taxonomies.taxonomy_uid", "term_uid", 3)
.toJSON()
.find()
.then(response => {
// the response
data = response;
})equalAndAbove
The equalAndAbove method retrieves all entries for a specific taxonomy that match a specific term and all its ancestor terms, requiring only the target term and a specified level.
NoteIf you don't specify the level, the default behavior is to retrieve terms up to level 10.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the taxonomy field, specified as taxonomies.<taxonomy_uid> |
| value (required) | string | UID of the term to match or compare |
| levels | int | Depth level till which terms will be matched |
Example:
let data;
stack
.Taxonomies()
.equalAndAbove("taxonomies.taxonomy_uid", "term_uid", 3)
.toJSON()
.find()
.then(response => {
// the response
data = response;
})above
The above method retrieves all entries for a specific taxonomy that match only the parent term(s) of a specified target term, excluding the target term itself and a specified level.
NoteIf you don't specify the level, the default behavior is to retrieve terms up to level 10.
| Name | Type | Description |
|---|---|---|
| key (required) | string | UID of the taxonomy field, specified as taxonomies.<taxonomy_uid> |
| value (required) | string | UID of the term to match or compare |
| levels | int | Depth level till which terms will be matched |
Example:
let data;
stack
.ContentType('ct_uid')
.Query()
.above("taxonomies.taxonomy_uid", "term_uid", 3)
.toJSON()
.find()
.then(response => {
// the response
data = response;
})