---
title: "Query"
description: "Query"
url: "https://www.contentstack.com/docs/developers/sdks/content-delivery-sdk/ruby/reference/query"
product: "Contentstack"
doc_type: "guide"
audience:
  - developers
  - admins
version: "current"
last_updated: "2026-09-21"
---

# Query

## Query

A class that defines a query that is used to query for Entry instance.

## add_query

Add a custom query against specified key.

```
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;
```

Field uid to be constraint on

Value for constraint

## remove_query

Combines all the queries together using AND operator

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.remove_query('author')
	.fetch;
```

Field uid to be constraint on

## where

Add a constraint to fetch all entries that contains given value against specified key.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.where({:author => "Jane Doe"})
	.fetch;
```

Query hash for equal condition

## not_equal_to

Add a constraint to the query that requires a particular key's entry to be not equal to the provided value.

```
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;
```

UID of the field for which query should be executed

The object that must not be equaled.

## contained_in

Add a constraint to the query that requires a particular key's entry to be contained in the provided array.

```
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;
```

UID of the field for which query should be executed

The possible values for the key's object

## not_contained_in

Add a constraint to the query that requires a particular key entry's value not be contained in the provided array.

```
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;
```

UID of the field for which query should be executed

The possible values for the key's object

## include_reference

Add a constraint that requires a particular reference key details.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.include_reference('category')
	.fetch;
```

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', 'review'])
	.fetch;
```

Pass array of reference fields that must be included in the response

## 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:

```
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;
```

The key to be constrained.

The regular expression pattern to match.

Regex options

## and

Combines all the queries together using AND operator

```
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;
```

Array of instances of the Query class

## or

Add a constraint to fetch all entries which satisfy any queries.

```
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
@query2.where("title", "Apparel")

query_array = [@query1, @query2]

@entries = @stack.content_type("content_type_uid").query
	.or(query_array)
	.fetch;
```

Array of instances of the Query class

## less_than

Add a constraint to the query that requires a particular key entry to be less than the provided value.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.less_than('age', 20)
	.fetch;
```

UID of the field

Value used to match or compare

## less_than_or_equal

Add a constraint to the query that requires a particular key entry to be less than or equal to the provided value.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.less_than_or_equal('age', 20)
	.fetch;
```

UID of the field

Value used to match or compare

## greater_than

Add a constraint to the query that requires a particular key entry to be greater than the provided value.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.greater_than('age', 20)
	.fetch;
```

UID of the field

Value used to match or compare

## greater_than_or_equal

Add a constraint to the query that requires a particular key entry to be greater than or equal to the provided value.

```
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;
```

UID of the field

Value used to match or compare

## limit

A limit on the number of objects to return.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.limit(20)
	.fetch;
```

Objects to limit in result set.

## skip

The number of objects to skip before returning any.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.skip(20)
	.fetch;
```

Objects to skip in result set.

## ascending

Sort the results in ascending order with the given key. Sort the returned entries in ascending order of the provided key.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.ascending("field_uid")
	.fetch;
```

The key to order by

## descending

Sort the results in descending order with the given key. Sort the returned entries in descending order of the provided key.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.descending("field_uid")
	.fetch;
```

The key to order by

## exists

Add a constraint that requires, a specified key exists in response.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.exists('product_image')
	.fetch;
```

The key to be constrained.

## not_exists

Add a constraint that requires, a specified key does not exists in response.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.not_exists('product_image')
	.fetch;
```

The key to be constrained.

## only

Specifies an array of 'only' keys in BASE object that would be 'included' in the response.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.only(['title', 'description'])
	.fetch;
```

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;
```

Reference field uid for which fields to be included

Array of the 'only' reference keys to be included in response.

## except

Specifies list of field uids that would be 'excluded' from the response.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.except(['title', 'description'])
	.fetch;
```

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;
```

Reference field uid for which fields to be excluded

Array of the 'only' reference keys to be excluded in response.

## 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

Include tags with which to search entries.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.tags(["tag1", "tag2"])
	.fetch;
```

Array of tags using which search must be performed

## locale

Get entries from the specified locale.

```
require "contentstack";

@stack = Contentstack::Client.new("api_key", "delivery_token", "environment");
@entries = @stack.content_type("content_type_uid").query
	.locale("en-us")
	.fetch;
```

The locale code of the entry

## 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;
```

## Query

Returns the value of attribute content\_type.

Returns the value of attribute query.

Returns the value of attribute content\_type.

Returns the value of attribute query.

## Query | Ruby Delivery SDK | Contentstack

The Query class in the Ruby Delivery SDK defines queries used to search and filter Entry instances from your Contentstack stack.