---
title: Result Sets
description: How to use Couchbase Lite Query's Result Sets
pubDate: 2026-08-17T09:53:44.266Z
antora:
  editUrl: https://github.com/couchbase/docs-couchbase-lite/edit/release/3.0/modules/android/pages/query-resultsets.adoc
  xref: xref:3.0@couchbase-lite:android:query-resultsets.adoc[]
---

[Consult the llms.txt file for a full list of contents](/llms.txt)
[View original HTML](/couchbase-lite/3.0/android/query-resultsets.html)

# Result Sets

> Description — _How to use Couchbase Lite Query's Result Sets_  
> Related Content — [QueryBuilder](querybuilder.md) | [SQL++ for Mobile](query-n1ql-mobile.md) | [Predictive Queries](querybuilder.md#lbl-predquery) | [Live Queries](query-live.md) | [Indexing](indexing.md)

## [](#query-execution)Query Execution

The execution of a Couchbase Lite for Android's database query returns an array of results, a result set.

Each row of the result set represents the data returned from a document that met the conditions defined by the `WHERE` statement of your query. The composition of each row is determined by the `SelectResult` expressions provided in the `SELECT` statement.

## [](#lbl-rtnd-res)Returned Results

[Return All Document Properties](#lbl-rtn-all)| [Return Document Id Only](#lbl-rtn-id)| [Return Specific Properties Only](#lbl-rtn-specific)

The types of SelectResult formats you may encounter include those generated by :

* `QueryBuilder.select(SelectResult.all())` — [Using All](#lbl-rtn-all)
* `QueryBuilder.select(SelectResult.expression(Meta.id))` — [Using Doc Id](#lbl-rtn-id) Metadata such as the `_id`
* `QueryBuilder.select(SelectResult.property("myProp"))` — [Using Specific Properties](#lbl-rtn-specific)

### [](#lbl-rtn-all)Return All Document Properties

The SelectResult returned by `SelectResult.all()` is a dictionary object, with the database name as the key and the document properties as an array of key-value pairs

Example 1\. Returning All Properties

```json
[
  {
    "travel-sample": { (1)
      "callsign": "MILE-AIR",
      "country": "United States",
      "iata": "Q5",
      "icao": "MLA",
      "id": 10,
      "name": "40-Mile Air",
      "type": "airline"
    }
  },
  {
    "travel-sample": { (2)
      "callsign": "ALASKAN-AIR",
      "country": "United States",
      "iata": "AA",
      "icao": "AAA",
      "id": 10,
      "name": "Alaskan Airways",
      "type": "airline"
    }
  }
]
```

### [](#lbl-rtn-id)Return Document Id Only

The SelectResult returned by queries using a SelectResult expression of the form `SelectResult.expression(Meta.id)` comprises a dictionary object with `ID` as the key and the ID value as the value.

Example 2\. Returning Meta Properties — Document ID

```json
[
  {
    "id": "hotel123"
  },
  {
    "id": "hotel456"
  },
]
```

### [](#lbl-rtn-specific)Return Specific Properties Only

The SelectResult returned by queries using one or more SelectResult expressions of the form `SelectResult.expression(property("name")) )` comprises a key-value pair for each SelectResult expression in the query. The key being the property name.

Example 3\. Returning Specific Properties

```json
[
  { (1)
    "id": "hotel123",
    "type": "hotel",
    "name": "Hotel Ghia"
  },
  { (2)
    "id": "hotel456",
    "type": "hotel",
    "name": "Hotel Deluxe",
  }
]
```

## [](#lbl-process-resultset)Processing Results

[Access Document Properties - All Properties](#lbl-acc-all)| [Access Document Properties - ID](#lbl-acc-id)| [Access Document Properties - Selected Properties](#lbl-acc-specific)

To retrieve the results of your query, you need to execute it using `Query.execute`.

The output from the execution is an array, with each array element representing the data from a document that matched your search criteria.

To unpack the results you need to iterate through this array. Alternatively, you can convert the result to a JSON string — see:

### [](#lbl-acc-all)Access Document Properties - All Properties

Here we look at how to access document properties when you have used SelectResult.all.

In this case each array element is a dictionary structure with the database name as its key. The properties are presented in the value as an array of key-value pairs (property name/property value).

You access the retrieved document properties by converting each row's value, in turn, to a dictionary — as shown in [Example 4](#ex-acc-all).

Example 4\. Access All Properties

* Kotlin
* Java

```Kotlin
val hotels: HashMap<String, Hotel> = HashMap()

for (result in listQuery.execute().allResults()) {
    // get the k-v pairs from the 'hotel' key's value into a dictionary
    val thisDocsProps = result.getDictionary(0) (1)
    val thisDocsId = thisDocsProps!!.getString("id")
    val thisDocsName = thisDocsProps.getString("name")
    val thisDocsType = thisDocsProps.getString("type")
    val thisDocsCity = thisDocsProps.getString("city")

    // Alternatively, access results value dictionary directly
    val id = result.getDictionary(0)?.getString("id").toString() (2)
    hotels[id] = Hotel(
        id,
        result.getDictionary(0)?.getString("type"),
        result.getDictionary(0)?.getString("name"),
        result.getDictionary(0)?.getString("city"),
        result.getDictionary(0)?.getString("country"),
        result.getDictionary(0)?.getString("description")
    )
}
```

```Java
try {
    for (Result result : listQuery.execute().allResults()) {
                     // get the k-v pairs from the 'hotel' key's value into a dictionary
        thisDocsProps = result.getDictionary(0); (1)
        thisDocsId = thisDocsProps.getString("id");
        thisDocsName = thisDocsProps.getString("Name");
        thisDocsType = thisDocsProps.getString("Type");
        thisDocsCity = thisDocsProps.getString("City");

        // Alternatively, access results value dictionary directly
        final Hotel hotel = new Hotel();
        hotel.Id = result.getDictionary(0).getString("id"); (2)
        hotel.Type = result.getDictionary(0).getString("Type");
        hotel.Name = result.getDictionary(0).getString("Name");
        hotel.City = result.getDictionary(0).getString("City");
        hotel.Country= result.getDictionary(0).getString("Country");
        hotel.Description = result.getDictionary(0).getString("Description");
        hotels.put(hotel.Id, hotel);
    }

} catch (CouchbaseLiteException e) {
    e.printStackTrace();
}
```

| **1** | Here we get the dictionary of document properties using the database name as the key. You can add this dictionary to an array of returned matches, for processing elsewhere in the app. |
| ----- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **2** | Alternatively, you can access the document properties here, by using the property names as keys to the dictionary object.                                                               |

### [](#lbl-acc-id)Access Document Properties - ID

Here we look at how to access document properties when you have returned only the document IDs for documents that matched your selection criteria.

This is something you may do when retrieval of the properties directly by the query may consume excessive amounts of memory and-or processing time.

In this case each array element is a dictionary structure where `ID` is the key and the required document ID is the value.

Access the required document properties by retrieving the document from the database using its document ID — as shown in [Example 5](#ex-acc-id).

Example 5\. Access by ID

* Kotlin
* Java

```Kotlin
for (result in rs.execute().allResults()) {
  Log.i(TAG, "hotel id ->${result.getString("hotelId")}")
}
```

```Java

try {
    for (Result result : listQuery.execute().allResults()) {

        // get the ID form the result's k-v pair array
        thisDocsId = result.getString("metaID"); (1)

        // Get document from DB using retrieved ID
        Document thisDoc = this_Db.getDocument(thisDocsId);

        // Process document as required
        thisDocsName = thisDoc.getString("Name");

    }
} catch (CouchbaseLiteException e) {
    e.printStackTrace();
}
```

| **1** | Extract the Id value from the dictionary and use it to get the document from the database |
| ----- | ----------------------------------------------------------------------------------------- |

### [](#lbl-acc-specific)Access Document Properties - Selected Properties

Here we look at how to access properties when you have used SelectResult to get a specific subset of properties.

In this case each array element is an array of key value pairs (property name/property value).

Access the retrieved properties by converting each row into a dictionary — as shown in [\[ex-acc-specific\]](#ex-acc-specific).

* Kotlin
* Java

```Kotlin
for (result in rs.execute().allResults()) {
    Log.i(TAG, "Hotel name -> ${result.getString("name")}, in ${result.getString("country")}" )
}
```

```Java

try {
  for (Result result : listQuery.execute().allResults()) {

    // get data direct from result k-v pairs
    final Hotel hotel = new Hotel();
    hotel.Id = result.getString("id");
    hotel.Type = result.getString("Type");
    hotel.Name = result.getString("Name");
    hotel.City = result.getString("City");

    // Store created hotel object in a hashmap of hotels
    hotels.put(hotel.Id, hotel);

    // Get result k-v pairs into a 'dictionary' object
    Map <String, Object> thisDocsProps = result.toMap();
    thisDocsId =
    thisDocsProps.getOrDefault("id",null).toString();
    thisDocsName =
    thisDocsProps.getOrDefault("Name",null).toString();
    thisDocsType =
    thisDocsProps.getOrDefault("Type",null).toString();
    thisDocsCity =
    thisDocsProps.getOrDefault("City",null).toString();

  }
} catch (CouchbaseLiteException e) {
  e.printStackTrace();
}
```

## [](#json-result-sets)JSON Result Sets

Example 6\. Using JSON Results

Use [Result.toJSON()](http://docs.couchbase.com/mobile/3.0.15/couchbase-lite-android/com/couchbase/lite/Result.html#toJSON--) to transform your result string into a JSON string, which can easily be serialized or used as required in your application. See <\> for a working example. 

* Kotlin
* Java

```Kotlin
// Uses Jackson JSON processor
val mapper = ObjectMapper()
val hotels: ArrayList<Hotel> = ArrayList()

for (result in listQuery.execute()) {

    // Get result as JSON string
    val json = result.toJSON()

    // Get Hashmap from JSON string
    val dictFromJSONstring = mapper.readValue(json, HashMap::class.java)

    // Use created hashmap
    val hotelId = dictFromJSONstring["id"].toString() //
    val hotelType = dictFromJSONstring["type"].toString()
    val hotelname = dictFromJSONstring["name"].toString()


    // Get custom object from JSON string
    val thisHotel = mapper.readValue(json, Hotel::class.java)
    hotels.add(thisHotel)
}
```

```Java
// Uses Jackson JSON processor

ArrayList<Hotel> hotels = new ArrayList<Hotel>();
HashMap<String, Object> dictFromJSONstring;
for (Result result : listQuery.execute()) {

  // Get result as JSON string
  String thisJsonString = result.toJSON();

  // Get Java  Hashmap from JSON string
  HashMap<String, Object> dictFromJSONstring =
          mapper.readValue(thisJsonString, HashMap.class);


  // Use created hashmap
  String hotelId = dictFromJSONstring.get("id").toString();
  String hotelType = dictFromJSONstring.get("type").toString();
  String hotelname = dictFromJSONstring.get("name").toString();


  // Get custom object from JSON string
  Hotel thisHotel =
          mapper.readValue(thisJsonString, Hotel.class);
  hotels.add(thisHotel);
```

JSON String Format

If your query selects ALL then the JSON format will be:

```JSON
{
  database-name: {
    key1: "value1",
    keyx: "valuex"
  }
}
```

If your query selects a sub-set of available properties then the JSON format will be:

```JSON
{
  key1: "value1",
  keyx: "valuex"
}
```

## [](#related-content)Related Content

###### [](#)

How to . . .

* [Prerequisites](gs-prereqs.md)
* [Install](gs-install.md)
* [Build and Run](gs-build.md)

###### [](#-2)

Learn more . . .

* [Databases](database.md)
* [Documents](document.md)
* [Blobs](blob.md)
* [Remote Sync Gateway](replication.md)
* [Handling Data Conflicts](conflict.md)

###### [](#-3)

Dive Deeper . . .

[Mobile Forum](https://forums.couchbase.com/c/mobile/14) | [Blog](https://blog.couchbase.com/) | [Tutorials](https://docs.couchbase.com/tutorials/)