Query
Query
A class that defines a query that is used to query for Entry instance.
| Name | Type | Description |
|---|---|---|
| content_type | String | Returns the value of attribute content_type. |
| query | Object | Returns the value of attribute query. |
add_query
| Name | Type | Description |
|---|---|---|
| field_uid | String | Field uid to be constraint on |
| value | (String/Number/Boolean/Hash) | Value for constraint |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.add_query('author', "Jane Doe")
.fetch;remove_query
| Name | Type | Description |
|---|---|---|
| field_uid | String | Field uid to be constraint on |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.remove_query('author')
.fetch;where
Add a constraint to fetch all entries that contains given value against specified key.
| Name | Type | Description |
|---|---|---|
| query_hash | Hash | Query hash for equal condition |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.where({:author => "Jane Doe"})
.fetch;not_equal_to
| Name | Type | Description |
|---|---|---|
| field_uid | String | UID of the field for which query should be executed |
| value | String | The object that must not be equaled. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.not_equal_to('title', 'some random title')
.fetch;contained_in
| Name | Type | Description |
|---|---|---|
| field_uid | String | UID of the field for which query should be executed |
| value | (String/Number) | The possible values for the key's object |
Array equals operator on field uid
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.contained_in("title", ["Electronics", "Apparel"])
.fetch;Array equals operator Within Modular Blocks
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.contained_in("additional_info.deals.deal_name", ["Electronics", "Apparel"])
.fetch;not_contained_in
| Name | Type | Description |
|---|---|---|
| field_uid | String | UID of the field for which query should be executed |
| value | (String/Number) | The possible values for the key's object |
Array Not-equals Operator on field uid
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.not_contained_in("title", ["Electronics", "Apparel"])
.fetch;Array Not-equals Operator Within Modular Blocks
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.not_contained_in("additional_info.deals.deal_name", ["Electronics", "Apparel"])
.fetch;include_reference
Add a constraint that requires a particular reference key details.
| Name | Type | Description |
|---|---|---|
| reference_field_uids | string | Pass reference field that must be included in the response |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.include_reference('category')
.fetch;| Name | Type | Description |
|---|---|---|
| reference_field_uids | Array | Pass array of reference fields that must be included in the response |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.include_reference(['category', 'review'])
.fetch;regex
Add a regular expression constraint for finding string values that match the provided regular expression. This may be slow for large data sets. Example:
| Name | Type | Description |
|---|---|---|
| field_uid | string | The key to be constrained. |
| pattern | String | The regular expression pattern to match. |
| options | String | Regex options |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.regex('title', '.*Mobile.*')
.fetch;require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.regex('title', '.*Mobile.*', 'i')
.fetch;and
| Name | Type | Description |
|---|---|---|
| queries | Array | Array of instances of the Query class |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");@query1 = @stack.content_type("category").query
@query1.where("title", "Apparel")
@query2 = @stack.content_type("category").query
<span>@query2.where("title", "Apparel")</span>
query_array = [@query1, @query2]
@entries = @stack.content_type("content_type_uid").query
.and(query_array)
.fetch;or
| Name | Type | Description |
|---|---|---|
| queries | Array | Array of instances of the Query class |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");@query1 = @stack.content_type("category").query
@query1.where("title", "Apparel")
@query2 = @stack.content_type("category").query
query_array = [@query1, @query2]
@entries = @stack.content_type("content_type_uid").query
.or(query_array)
.fetch;less_than
| Name | Type | Description |
|---|---|---|
| field_uid | String | UID of the field |
| value | (String/Number) | Value used to match or compare |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.less_than('age', 20)
.fetch;less_than_or_equal
| Name | Type | Description |
|---|---|---|
| field_uid | String | UID of the field |
| value | (String/Number) | Value used to match or compare |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.
('age', 20)
.fetch;greater_than
| Name | Type | Description |
|---|---|---|
| field_uid | String | UID of the field |
| value | (String/Number) | Value used to match or compare |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.greater_than('age', 20)
.fetch;greater_than_or_equal
| Name | Type | Description |
|---|---|---|
| field_uid | String | UID of the field |
| value | (String/Number) | Value used to match or compare |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.greater_than_or_equal('age', 20)
.fetch;limit
A limit on the number of objects to return.
| Name | Type | Description |
|---|---|---|
| count | Number | Objects to limit in result set. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.limit(20)
.fetch;skip
The number of objects to skip before returning any.
| Name | Type | Description |
|---|---|---|
| count | Number | Objects to skip in result set. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.skip(20)
.fetch;ascending
Sort the results in ascending order with the given key. Sort the returned entries in ascending order of the provided key.
| Name | Type | Description |
|---|---|---|
| field_uid | String | The key to order by |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.ascending("field_uid")
.fetch;descending
Sort the results in descending order with the given key. Sort the returned entries in descending order of the provided key.
| Name | Type | Description |
|---|---|---|
| field_uid | String | The key to order by |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.descending("field_uid")
.fetch;exists
Add a constraint that requires, a specified key exists in response.
| Name | Type | Description |
|---|---|---|
| field_uid | String | The key to be constrained. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.exists('product_image')
.fetch;not_exists
Add a constraint that requires, a specified key does not exists in response.
| Name | Type | Description |
|---|---|---|
| field_uid | String | The key to be constrained. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.not_exists('product_image')
.fetch;only
Specifies an array of 'only' keys in BASE object that would be 'included' in the response.
| Name | Type | Description |
|---|---|---|
| fields | Array | Array of the 'only' reference keys to be included in response. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.only(['title', 'description'])
.fetch;| Name | Type | Description |
|---|---|---|
| fields | String | Reference field uid for which fields to be included |
| fields_with_base | Array | Array of the 'only' reference keys to be included in response. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.only('category', ['title', 'description'])
.fetch;except
Specifies list of field uids that would be 'excluded' from the response.
| Name | Type | Description |
|---|---|---|
| fields | Array | Array of the 'only' reference keys to be excluded in response. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.except(['title', 'description'])
.fetch;| Name | Type | Description |
|---|---|---|
| fields | String | Reference field uid for which fields to be excluded |
| fields_with_base | Array | Array of the 'only' reference keys to be excluded in response. |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.except('category', ['title', 'description'])
.fetch;include_count
Retrieve count and data of objects in result.
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entry = @stack.content_type("content_type_uid").query
.include_count
.fetch;tags
| Name | Type | Description |
|---|---|---|
| tags_array | Array | Array of tags using which search must be performed |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.tags(["tag1", "tag2"])
.fetch;locale
Get entries from the specified locale.
| Name | Type | Description |
|---|---|---|
| code | String | The locale code of the entry |
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.locale("en-us")
.fetch;include_content_type
Include object's content_type in response
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.include_content_type
.fetch;include_branch
Include the branch for publish content.
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entry = @stack.content_type("content_type_uid").query
.include_branch
.fetch;include_fallback
Include the fallback locale publish content, if specified locale content is not publish.
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.include_fallback
.fetch;include_embedded_items
Include Embedded Objects (Entries and Assets) along with entry/entries details.
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
.include_embedded_items
.fetch;find
Fetches the array of the entries from Contentstack for specific ContentType
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query.find;find_one
Execute a Query and get the single matching object
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query.find_one;count
Retrieve only count of entries in result.
require "contentstack";
@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@count = @stack.content_type("content_type_uid").query.count;