Query

Query

new Query() → {Query}

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

Query instance creation.

let Query = Contentstack.Stack().ContentType('example').Query();
let assetQuery =  Contentstack.Stack().Assets().Query();

Methods

lessThan(key, value) → {Query}

Retrieves entries in which the value of a field is lesser than the provided value
Parameters:
Name Description
key uid of the field
value Value used to match or compare
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.lessThan('created_at','2015-06-22').find()
         data.then(function (result) {
         // result content the data who's 'created_at date' is less than '2015-06-22'
      },function (error) {
         // error function
     })

lessThanOrEqualTo(key, value) → {Query}

Retrieves entries in which the value of a field is lesser than or equal to the provided value.
Parameters:
Name Description
key uid of the field
value Value used to match or compare
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.lessThanOrEqualTo('created_at','2015-06-22').find()
         data.then(function (result) {
         // result contain the data of entries where the 'created_at' date will be less than or equalto '2015-06-22'.
      },function (error) {
         // error function
     })

greaterThan(key, value) → {Query}

Retrieves entries in which the value for a field is greater than the provided value.
Parameters:
Name Description
key uid of the field
value value used to match or compare
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.greaterThan('created_at','2015-03-12').find()
                    data.then(function(result) {
                      // result contains the data of entries where the 'created_at' date will be greaterthan '2015-06-22'
                    },function (error) {
                      // error function
                    })

greaterThanOrEqualTo(key, value) → {Query}

Retrieves entries in which the value for a field is greater than or equal to the provided value.
Parameters:
Name Description
key uid of the field
value Value used to match or compare
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.greaterThanOrEqualTo('created_at','2015-03-12').find()
         data.then(function(result) {
         // result contains the data of entries where the 'created_at' date will be greaterThan or equalto '2015-06-22'
      },function (error) {
         // error function
     })

notEqualTo(key, value) → {Query}

Retrieves entries in which the value for a field does not match the provided value.
Parameters:
Name Description
key uid of the field
value Value used to match or compare
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.notEqualTo('title','Demo').find()
         data.then(function(result) {
           // ‘result’ contains the list of entries where value of the ‘title’ field will not be 'Demo'.
      },function (error) {
         // error function
     })

containedIn(key, value) → {Query}

Retrieve entries in which the value of a field matches with any of the provided array of values
Parameters:
Name Description
key uid of the field
value Array of values that are to be used to match or compare
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.containedIn('title', ['Demo', 'Welcome']).find()
         data.then(function(result) {
         // ‘result’ contains the list of entries where value of the ‘title’ field will contain either 'Demo' or ‘Welcome’.
      },function (error) {
         // error function
     })

notContainedIn(key, value) → {Query}

Retrieve entries in which the value of a field does not match with any of the provided array of values.
Parameters:
Name Description
key uid of the field
value Array of values that are to be used to match or compare
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.notContainedIn('title', ['Demo', 'Welcome']).find()
         data.then(function(result) {
         // 'result' contains the list of entries where value of the title field should not be either "Demo" or ‘Welcome’
      },function (error) {
         // error function
     })

exists(key) → {Query}

Retrieve entries if value of the field, mentioned in the condition, exists.
Parameters:
Name Description
key uid of the field
Examples
blogQuery.exists('featured')
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.exists('featured').find()
         data.then(function(result) {
         // ‘result’ contains the list of entries in which "featured" exists.
      },function (error) {
         // error function
     })

notExists(key) → {Query}

Retrieve entries if value of the field, mentioned in the condition, does not exists.
Parameters:
Name Description
key uid of the field
Examples
blogQuery.notExists('featured')
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.notExists('featured').find()
         data.then(function(result) {
       // result is the list of non-existing’featured’" data.
      },function (error) {
         // error function
     })

ascending(key) → {Query}

Sort fetched entries in the ascending order with respect to a specific field.
Parameters:
Name Description
key field uid based on which the ordering will be done
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.ascending('created_at').find()
         data.then(function(result) {
          // ‘result’ contains the list of entries which is sorted in ascending order on the basis of ‘created_at’. 
      },function (error) {
         // error function
     })

descending(key) → {Query}

Sort fetched entries in the descending order with respect to a specific field
Parameters:
Name Description
key field uid based on which the ordering will be done.
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.descending('created_at').find()
         data.then(function(result) {
          // ‘result’ contains the list of entries which is sorted in descending order on the basis of ‘created_at’. 
      },function (error) {
         // error function
     })

beforeUid(uid) → {Query}

Sort fetched entries in the descending order with respect to a specific field
Parameters:
Name Description
uid field uid based on which the ordering will be done.
Example
blogQuery.beforeUid('blt1234567890abcdef')

afterUid(uid) → {Query}

This method provides only the entries after the specified entry id.
Parameters:
Name Description
uid uid of the entry
Example
blogQuery.afterUid('blt1234567890abcdef')

skip(skip) → {Query}

Skips at specific number of entries.
Parameters:
Name Description
skip number of entries to be skipped
Examples
blogQuery.skip(5)
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.skip(5).find()
         data.then(function(result) {
         // result contains the list of data which is sorted in descending order on 'created_at' bases. 
      },function (error) {
         // error function
     })

limit(limit) → {Query}

Returns a specific number of entries based on the set limit
Parameters:
Name Description
limit maximum number of entries to be returned
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.limit(10).find()
         data.then(function(result) {
         // result contains the limited number of entries
      },function (error) {
         // error function
     })

or(queries) → {Query}

Retrieves entries that satisfy at least one of the given conditions
Parameters:
Name Description
queries array of Query objects or raw queries
Examples

.or with Query instances

let Query1 = Stack.ContentType('blog').Query().where('title', 'Demo').find()
let Query2 = Stack.ContentType('blog').Query().lessThan('comments', 10).find()
blogQuery.or(Query1, Query2)

.or with raw queries

let Query1 = Stack.ContentType('blog').Query().where('title', 'Demo').getQuery()
let Query2 = Stack.ContentType('blog').Query().lessThan('comments', 10).getQuery()
blogQuery.or(Query1, Query2)

and(queries) → {Query}

Retrieve entries that satisfy all the provided conditions.
Parameters:
Name Description
queries array of query objects or raw queries.
Examples

.and with Query instances

let Query1 = Stack.ContentType('blog').Query().where('title', 'Demo')
let Query2 = Stack.ContentType('blog').Query().lessThan('comments', 10)
blogQuery.and(Query1, Query2)

.and with raw queries

let Query1 = Stack.ContentType('blog').Query().where('title', 'Demo').getQuery()
let Query2 = Stack.ContentType('blog').Query().lessThan('comments', 10).getQuery()
blogQuery.and(Query1, Query2)

where(key, value) → {Query}

Retrieve entries in which a specific field satisfies the value provided
Parameters:
Name Description
key uid of the field
value value used to match or compare
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.where('title','Demo').find()
         data.then(function(result) {
           // ‘result’ contains the list of entries where value of ‘title’ is equal to ‘Demo’. 
      },function (error) {
         // error function
     })

count() → {Query}

Returns the total number of entries
Examples
blogQuery.count()
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.count().find()
         data.then(function(result) {
          // ‘result’ contains the total count. 
      },function (error) {
         // error function
     })

query(query) → {Query}

Retrieve entries based on raw queries
Parameters:
Name Description
query RAW (JSON) queries
Example
let blogQuery = Stack().ContentType('example').Query();
let data = blogQuery.query({"brand": {"$nin_query": {"title": "Apple Inc."}}}).find()
data.then(function(result) {
   // ‘result’ contains the total count. 
},function (error) {
   // error function
})

referenceIn(query) → {Query}

Retrieve entries that satisfy the query conditions made on referenced fields.
Parameters:
Name Description
query RAW (JSON) queries
Examples

referenceIn with Query instances

let blogQuery = Stack().ContentType('example').Query();
let Query = Stack.ContentType('blog').Query().where('title', 'Demo')
let data = blogQuery.referenceIn("brand", Query).find()
data.then(function(result) {
   // ‘result’ contains the total count. 
},function (error) {
   // error function
})

referenceIn with raw queries

let blogQuery = Stack().ContentType('example').Query();
let data = blogQuery.referenceIn("brand", {'title': 'Demo'}).find()
data.then(function(result) {
   // ‘result’ contains the total count. 
},function (error) {
   // error function
})

referenceNotIn(query) → {Query}

Retrieve entries that does not satisfy the query conditions made on referenced fields.
Parameters:
Name Description
query RAW (JSON) queries
Examples

referenceNotIn with Query instances

let blogQuery = Stack().ContentType('example').Query();
let data = blogQuery.referenceNotIn("brand", {'title': 'Demo'}).find()
data.then(function(result) {
   // ‘result’ contains the total count. 
},function (error) {
   // error function
})

referenceNotIn with raw queries

let blogQuery = Stack().ContentType('example').Query();
let Query = Stack.ContentType('blog').Query().where('title', 'Demo')
let data = blogQuery.referenceNotIn("brand", Query).find()
data.then(function(result) {
   // ‘result’ contains the total count. 
},function (error) {
   // error function
})

tags(values) → {Query}

Retrieves entries based on the provided tags
Parameters:
Name Description
values tags
Example
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.tags(['technology', 'business']).find()
         data.then(function(result) {
       // ‘result’ contains list of entries which have tags "’technology’" and ‘"business’".
      },function (error) {
         // error function
     })

includeReferenceContentTypeUid() → {Query}

This method also includes the content type UIDs of the referenced entries returned in the response.
Examples
Stack.ContentType("contentType_uid").Query().includeReferenceContentTypeUID().find()
let blogQuery = Stack.ContentType("contentType_uid").Query();
         let data = blogQuery.includeReferenceContentTypeUID().find()
         data.then(function(result) {
        // ‘result’ contains a list of entries in which content type UIDs is present. 
      },function (error) {
         // error function
     })

includeCount() → {Query}

Includes the total number of entries returned in the response.
Examples
blogQuery.includeCount()
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.includeCount().find()
         data.then(function(result) {
        // ‘result’ contains a list of entries in which count of object is present at array[1] position. 
      },function (error) {
         // error function
     })

addParam() → {Query}

Includes query parameters in your queries.
Example
var data = blogQuery.addParam('include_count', 'true').fetch()
     data.then(function (result) {
         // 'result' is an object which content the data including count in json object form
      },function (error) {
         // error function
     })

getQuery() → {Query}

Returns the raw (JSON) query based on the filters applied on Query object.
Example
Stack.ContentType('contentType_uid').Query().where('title','Demo').getQuery().find()

regex(key, value, optionsopt) → {Query}

Retrieve entries that match the provided regular expressions
Parameters:
Name Attributes Description
key uid of the field
value value used to match or compare
options <optional>
match or compare value in entry
Examples

.regex without options

blogQuery.regex('title','^Demo')

.regex with options

blogQuery.regex('title','^Demo', 'i')
Retrieve entries that have fields which match the provided search value.
Parameters:
Name Description
value value to search in entries
Examples
blogQuery.search('Welcome to demo')
let blogQuery = Stack().ContentType('example').Query();
         let data = blogQuery.search('welcome to demo').find()
         data.then(function(result) {
        // ‘result’ contains the object that possess the text "’welcome to demo’".
      },function (error) {
         // error function
     })

find() → {promise}

Retrieves entries that satisfied the specified query
Examples
let blogQuery = Stack().ContentType('example').Query().find();
         blogQuery.then(function(result) {
         // result contains the list of object. 
      },function (error) {
         // error function
     })
blogQuery.find()
let blogQuery = Stack.ContentType(contentTypeUid).Query().find({
       
     });
blogQuery.then(function(result) {
         // result contains the list of object. 
      },function (error) {
         // error function
     })
blogQuery.find()

findOne() → {promise}

Retrieve a single entry from the result
Deprecated:
  • since verion 3.3.0
Example
let blogQuery = Stack().ContentType('example').Query().findOne();
         blogQuery.then(function(result) {
         // result contains the single item object. 
      },function (error) {
         // error function
     })
blogQuery.findOne()