Skip to content

Basic Object Retrieval

Backendless supports multiple data search and retrieval operations. These include finding an object by its objectId, finding first or last object in the collection or retrieving the entire persisted collection.

Retrieving Data Objects

Methods

Retrieve data objects with the default paging setting from a table:

Future<List<Map>> Backendless.data.of("TABLE-NAME").find([DataQueryBuilder queryBuilder]);
Find first data object from a table. The first data object is the first one saved in the data store:
Future<Map> Backendless.data.of("TABLE-NAME").findFirst({List<String> relations, int relationsDepth});
Find last data object from a table. The last data object is the last one saved in the data store:
Future<Map> Backendless.data.of("TABLE-NAME").findLast({List<String> relations, int relationsDepth});
Find a data object by its ID:
Future<Map> Backendless.data.of("TABLE-NAME").findById(String id,
      {List<String> relations,
      int relationsDepth,
      DataQueryBuilder queryBuilder});

Retrieve data objects with the default paging setting from a table. Returned list will contain objects of type E (the name of the class must match the name of the table):

Future<List<E>> Backendless.data.withClass<E>().find([DataQueryBuilder queryBuilder]);
Find first data object of class E. The first data object is the first one saved in the data store:
Future<E> Backendless.data.withClass<E>().findFirst({List<String> relations, int relationsDepth});
Find last data object of type E. The last data object is the last one saved in the data store:
Future<E> Backendless.data.withClass<E>().findLast({List<String> relations, int relationsDepth});
Find a data object by its ID:
Future<E> Backendless.data.withClass<E>().findById(String id,
    {List<String> relations,
    int relationsDepth,
    DataQueryBuilder queryBuilder});

where:

Argument                Description
TABLE-NAME Name of the table from where the data is retrieved from.
E Java class identifying the table where from where the data must be loaded from. For example, if the table is "Person", the argument should Person.class.

Signature: Find a data object by object  ID with queryBuilder parameter.

Future<E?> findById(String id,
    {List<String>? relations,
    int? relationsDepth,
    DataQueryBuilder? queryBuilder});

Example Non-Blocking Method: Find a data object by object  ID with the queryBuilder parameter.

DataQueryBuilder myQueryBuilder = DataQueryBuilder()
  ..related = ["relationA", "relationB"]
  ..relationsDepth = 1;

var response = await Backendless.data
    .of('TableName')
    .findById('objectId', queryBuilder: myQueryBuilder);
DataQueryBuilder myQueryBuilder = DataQueryBuilder()
  ..related = ["relationA", "relationB"]
  ..relationsDepth = 1;

var response = await Backendless.data
    .withClass<TableName>()
    .findById('objectId', queryBuilder: myQueryBuilder);

Example Async Callback Method: Find a data object by object  ID with the queryBuilder parameter.

DataQueryBuilder myQueryBuilder = DataQueryBuilder()
  ..related = ["relationA", "relationB"]
  ..relationsDepth = 1;

var response = await Backendless.data
    .of('TableName')
    .findById('objectId', queryBuilder: myQueryBuilder);
DataQueryBuilder myQueryBuilder = DataQueryBuilder()
  ..related = ["relationA", "relationB"]
  ..relationsDepth = 1;

var response = await Backendless.data
    .withClass<TableName>()
    .findById('objectId', queryBuilder: myQueryBuilder);

where:

Argument                Description
queryBuilder Instance of com.backendless.persistence.DataQueryBuilder. When present in the arguments, the object must contain a whereClause query. The query is used by the server to identify a collection of objects.
objectId object ID of the object to find. For the details about objectId, see the Data Object section of the documentation.
response handles successful result of an asynchronous call.
onError handles fault result of an asynchronous call.

Example

The following code demonstrates various search queries:

Load contacts using default paging

``` js
Backendless.data.of("Contact").find().then((foundContacts) {
  // every loaded object from the "Contact" table is now an individual Map
});
```

Find first contact

``` js
Backendless.data.of("Contact").findFirst().then((contact) {
  // first contact instance has been found
});
```

Find last contact

``` js
Backendless.data.of("Contact").findLast().then((contact) {
  // last contact instance has been found
});
```

Find contact by objectId

``` js
Map contact = {
  "name": "Jack Daniels",
  "age": 147,
  "phone": "777-777-777",
  "title": "Favorites",
};

Backendless.data.of("Contact").save(contact).then((savedContact) {
  // now retrieve the object using it's objectId
  Backendless.data.of("Contact").findById(savedContact["objectId"]).then((response) {
    // an object from the "Contact" table has been found by it's objectId
  }); 
});
```

Consider the following class:

import 'package:backendless_sdk/backendless_sdk.dart';

@reflector
class Contact {
  String objectId;
  String name;
  int age;
  String phone;
  String title;
}
The following code demonstrates various search queries:

Load contacts using default paging

``` js
Backendless.data.withClass<Contact>().find().then((foundContacts) {
  // every loaded object from the "Contact" table is now an individual Map
});
```

Find first contact

``` js
Backendless.data.withClass<Contact>().findFirst().then((contact) {
  // first contact instance has been found
});
```

Find last contact

``` js
Backendless.data.withClass<Contact>().findLast().then((contact) {
  // last contact instance has been found
});
```

Find contact by objectId

``` js
Contact contact = Contact()
  ..name = "Jack Daniels"
  ..age = 147
  ..phone = "777-777-777"
  ..title = "Favorites";

Backendless.data.withClass<Contact>().save(contact).then((savedContact) {
  // now retrieve the object using it's objectId
  Backendless.data.withClass<Contact>().findById(savedContact.objectId).then((response) {
    // a Contact instance has been found by objectId
  });
});
```

Codeless Reference

The data table employees presented below is used throughout all Codeless examples as the main reference:

data_service_example_data_table_basic_object_retrieval

Find First Object

The example below retrieves the first object stored in the "employees" data table.

data_service_get_first_object_from_table

where:

Argument                Description
table name Name of the data table from where the required object is retrieved.
relations Name of the related property to load. For example, if table employees has a relation column homeAddress pointing to an object in the Address table, the value of the parameter would be homeAddress. The syntax allows to add relations of relations. For example, if the same Address table has a relation country pointing to the Country table, then homeAddress.country would instruct the backend to load the related Country object.
relations depth Depth of the relations to include into the response.
properties Names of the properties/columns for which  to load the corresponding values.
exclude properties Names of the properties/columns that should not be included in the response.

The operation has returned the following result:

data_service_example_get_first_object_from_table

Find Last Object

The example below retrieves the last object stored in the "employees" data table.

data_service_get_last_object_from_table

The operation has returned the following result:

data_service_example_get_last_object_from_table

Find Object By ID

Consider the following scenario where you want to retrieve an object associated with both the object id: "2B6392CA-B720-4930-8E1C-14C7B06E4397" and the name "Alex Lincoln". In the example provided below, the operation searches for an object by its object id and returns it as part of the response:

data_service_get_object_by_objectId

where:

Argument                Description
table name Name of the data table from where the required object is retrieved.
object id Unique identifier of the object to retrieve.
relations Name of the related property to load. For example, if table employees has a relation column homeAddress pointing to an object in the Address table, the value of the parameter would be homeAddress. The syntax allows to add relations of relations. For example, if the same Address table has a relation country pointing to the Country table, then homeAddress.country would instruct the backend to load the related Country object.
relations depth Depth of the relations to include into the response.
properties Names of the properties/columns for which  to load the corresponding values.
exclude properties Names of the properties/columns that should not be included in the response.

The result of this operation will look as shown below after the Codeless logic runs.

data_service_example_find_object_by_id

Load All Objects From Data Table

The example below loads all objects from the employees data table.

data_service_load_objects

where:

Argument                Description
table name Name of the data table from where the objects are retrieved.
where clause A search query used by the server it to determine objects matching the condition. Refer to the Search With The Where Clause topic for more information.
having clause Sets a condition on a aggregate function to filter groups.
relations Name of the related property to load. For example, if table employees has a relation column homeAddress pointing to an object in the Address table, the value of the parameter would be homeAddress. The syntax allows to add relations of relations. For example, if the same Address table has a relation country pointing to the Country table, then homeAddress.country would instruct the backend to load the related Country object.
properties Names of the properties/columns for which  to load the corresponding values.
exclude properties Names of the properties/columns that should not be included in the response.
relations depth Depth of the relations to include into the response.
relations page size Sets the number of related objects returned in the response.
sort by Lists properties by which the returned collection should be sorted by.
group by Sets the name of the columns to group the results by.
page size Sets the page size which is the number of objects to be returned in the response.
page offset Zero-based index of the object in the persistent store from which to run the search. This parameter should be used when implementing paged access to data. Suppose the first request returned 20 objects (if pageSize is set to 20) and there are 100 objects total. The subsequent request can set offset to 20, so the next batch of objects is loaded sequentially.
distinct Used to return only unique values from a column.
file reference prefix This property allows replacing the default URL file prefix. For instance, when the operation returns a path to a file stored on the server ("https://yourdomain.backendless.app/my-file.jpg"), then you can reconstruct it by passing the new file name that must start with a slash - "/wonderful_forest.jpg". It is useful when you want the client application to open a specific file locally.

The result of this operation will look as shown below after the Codeless logic runs.

data_service_example_load_objects_2