Query

View as Markdown

Query

An initializer is responsible for creating Query object. Provides support for all search queries

addParam

Includes query parameters in your queries.

NameTypeDescription
keystring

URL parameter key

valuestring

Value corresponding to the param

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();

where

Retrieve entries in which a specific field satisfies the value provided.

NameTypeDescription
keystring

UID of the field

valueany

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().where('title', 'some random title').toJSON().find();

notEqualTo

Retrieves entries in which the value for a field does not match the provided value.

NameTypeDescription
keystring

UID of the field

valueany

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

Retrieve entries in which the value of a field matches with any of the provided array of values

NameTypeDescription
keystring

UID of the field

valuearray

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

Retrieve entries in which the value of a field does not match with any of the provided array of values.

NameTypeDescription
keystring

UID of the field

valuearray

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

Retrieve entries that satisfy all the provided conditions.

NameTypeDescription
queriesarray

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

Retrieves entries that satisfy at least one of the given conditions

NameTypeDescription
queriesarray

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

Retrieves entries in which the value of a field is lesser than the provided value

NameTypeDescription
keystring

UID of the field

valueany

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

Retrieves entries in which the value of a field is lesser than or equal to the provided value.

NameTypeDescription
keystring

UID of the field

valueany

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();

greaterThan

Retrieves entries in which the value for a field is greater than the provided value.

NameTypeDescription
keystring

UID of the field

valueany

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().greaterThan('age', 20).toJSON().find();

greaterThanOrEqualTo

Retrieves entries in which the value for a field is greater than or equal to the provided value.

NameTypeDescription
keystring

UID of the field

valueany

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

Returns a specific number of entries based on the set limit

NameTypeDescription
limitnumber

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();

skip

Skips at specific number of entries.

NameTypeDescription
skipnumber

number of entries to be skipped

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().skip(20).toJSON().find();

ascending

Sort fetched entries in the ascending order with respect to a specific field.

NameTypeDescription
keystring

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

Sort fetched entries in the descending order with respect to a specific field

NameTypeDescription
keystring

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

Retrieve entries if value of the field, mentioned in the condition, exists.

NameTypeDescription
keystring

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

Retrieve entries if value of the field, mentioned in the condition, does not exists.

NameTypeDescription
keystring

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

Displays values of only the specified fields of entries or assets in the response

NameTypeDescription
valuesstring|array

Field UID of content type

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

Displays all data of an entries or assets excluding the data of the specified fields.

NameTypeDescription
valuesstring|array

Field UID of content type

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

Retrieve entries that match the provided regular expressions

NameTypeDescription
keystring

uid of the field

valueany
value used to match or compare
optionsany

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

Retrieves entries based on the provided tags.

NameTypeDescription
valuesarray

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.

NameTypeDescription
valuestring|array

Field UID of content type

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

Retrieve entries based on raw queries.

NameTypeDescription
fieldUidstring

Reference field Uid

queryobject

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

Retrieve entries based on raw queries.

NameTypeDescription
fieldUidstring

Reference field Uid

queryobject

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

This method also includes the content type UIDs of the referenced entries returned in the response.

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

Includes the total number of entries returned in the response.

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

Include the details of the content type along with the entry/entries details.

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();

Your response should look like this:

[

  [

    {

      "uid": "blt3458ba16457349d2",

      "locale": "en-us",

      "_in_progress": false,

      "created_at": "2024-09-24T21:37:12.148Z",

      "created_by": "bltcd145d6b20c55b33",

      "title": "Test1",

      "updated_at": "2024-11-11T23:56:55.437Z",

      "updated_by": "bltcd145d6b20c55b33",

      "publish_details": {

        "time": "2024-11-11T23:56:59.408Z",

        "user": "bltcd145d6b20c55b33",

        "environment": "blt47ebc22ff5bb18d9",

        "locale": "en-us"

      }

    }

  ],

  {

    "title": "ct1",

    "description": "",

    "schema": [

      {

        "data_type": "text",

        "display_name": "Title",

        "field_metadata": { "_default": true, "version": 3 },

        "mandatory": true,

        "uid": "title",

        "unique": true,

        "multiple": false,

        "non_localizable": false

      }

    ],

    "uid": "ct1",

    "created_at": "2024-09-24T21:23:33.625Z",

    "updated_at": "2024-09-24T21:24:42.841Z",

    "inbuilt_class": false,

    "_version": 2

  }

]

includeBranch

Include the Branch for publish content.

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

Include Embedded Objects (Entries and Assets) along with entry/entries details.

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

Include the fallback locale publish content, if specified locale content is not publish.

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

Retrieve entries based on raw queries.

NameTypeDescription
queryobject

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.

NameTypeDescription
keystring

Key of the query

valueany

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

Returns the raw (JSON) query based on the filters applied on Query object.

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

Sets the language code of which you want to retrieve data.

NameTypeDescription
language_codestring

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

Retrieves entries that satisfied the specified query.

NameTypeDescription
paramobject

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

Retrieves entries that satisfied the specified query.

NameTypeDescription
paramobject

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

Returns the total number of entries

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.

NameTypeDescription
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.

NameTypeDescription
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.

NameTypeDescription
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.

NameTypeDescription
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;
})