The execution of a Couchbase Lite database query returns an array of results, a
result set.
The result set format and its handling varies slightly depending on the type of
SelectResult expressions used. The result set formats you may encounter
include those generated by:
To process the results of a query, you first need to execute it using
Query.execute.
The result set of an aggregate query contains one result per aggregation group
— see Select Count Only.
The result set of a query returning document properties contains zero or more
results. Each result represents the data from a document that matched your
search criteria (the WHERE clause). The composition of each result is
determined by the combination of SelectResult expressions provided in
the SELECT clause.
The SELECT clause for this type of query, which returns all document
properties for each document matching the query criteria, is fairly
straightforward — see Example 1.
The result set returned by queries using SelectResult.all contains
dictionaries — one for each document matching the query criteria.
Each result contains a key-value pair, where the key is the database name and
the value is a dictionary representing each document's properties — see:
Example 2.
You would typically use this type of query if retrieval of document properties
directly would consume excessive amounts of memory and-or processing time — see:
Example 7.
The result set returned by queries using a SelectResult expression of the
form SelectResult.expression(Meta.id) contains dictionaries — one for each
document matching the query criteria. Each result contains the ID under the id
key — see Example 8.
The result set returned by a count such as
SelectResult.expression(Function_.count(Expression.all())).as('count')
contains dictionaries with a key-value pair. The key is the count name, as
defined using SelectResultAs.as — see: Example 11 for the format and
Example 10 for the query.
One way to handle pagination in high-volume queries is to retrieve the results
in batches. Use the LIMIT and OFFSET clauses to return a defined number of
results starting from a given offset — see: Example 13.
In Dart/Flutter you can use code generators to auto generate the code for
handing serialization. See
Creating model classes the json serializable way
for more information. The example below shows how this can be done using the
class Hotel with methods generated from the code generators. Also note when
updating your model classes you will be required to run this command from the
terminal to update your code generated classes:
flutter pub run build_runner build --delete-conflicting-outputs