Skip to content

Get Object Count

The Object Count API provides a way to obtain the following values from the server:

  • Number of objects in a table
  • Number of objects matching query
  • Number of related objects

Non-Blocking API

// get object count for all objects in the table
public void Backendless.Data.of( "TABLE-NAME" ).getObjectCount( 
                                                AsyncCallback<Integer> callback );

// get object count for all objects in the table which match the query
public void Backendless.Data.of( "TABLE-NAME" ).getObjectCount( 
                                                DataQueryBuilder queryBuilder,
                                                AsyncCallback<Integer> callback );
// get object count for all objects in the table
public void Backendless.Data.of( E ).getObjectCount( AsyncCallback<Integer> callback );

// get object count for all objects in the table which match the query
public void Backendless.Data.of( E ).getObjectCount( DataQueryBuilder queryBuilder,
                                                     AsyncCallback<Integer> callback );

Blocking API

// get object count for all objects in the table
public int Backendless.Data.of( "TABLE-NAME" ).getObjectCount();

// get object count for all objects in the table which match the query
public int Backendless.Data.of( "TABLE-NAME" ).getObjectCount( DataQueryBuilder queryBuilder );
// get object count for all objects in the table
public int Backendless.Data.of( E ).getObjectCount();

// get object count for all objects in the table which match the query
public int Backendless.Data.of( E ).getObjectCount( DataQueryBuilder queryBuilder );

where:

Argument                Description
TABLE-NAME Name of the table where to calculate the object count.
E Java class of the data object which identifies the table where to calculate the object count.
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.
callback a callback object which will receive a callback when the method successfully calculates the object count. Applies to the asynchronous method only.

Return Value

The synchronous methods returns an integer value which is the object count. A asynchronous call receives a callback with the object count value.

Example

Total object count for a table

The following sample request retrieves total number of objects in table Order:

Backendless.Data.of( "Order" ).getObjectCount( new AsyncCallback<Integer>()
{
  @Override
  public void handleResponse( Integer integer )
  {
    Log.i( "MYAPP", "total objects in the Order table - " + integer );
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    Log.i( "MYAPP", "error - " + backendlessFault.getMessage() );
  }
} );
Backendless.Data.of( Order.class ).getObjectCount( new AsyncCallback<Integer>()
{
  @Override
  public void handleResponse( Integer integer )
  {
    Log.i( "MYAPP", "total objects in the Order table - " + integer );
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    Log.i( "MYAPP", "error - " + backendlessFault.getMessage() );
  }
} );

Object count for a query

The following sample request retrieves total number of objects in table Order which satisfy the condition of orderAmount > 100:

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "orderAmount > 100" );
Backendless.Data.of( "Order" ).getObjectCount( queryBuilder,
                                               new AsyncCallback<Integer>()
{
  @Override
  public void handleResponse( Integer integer )
  {
    Log.i( "MYAPP", "found objects " + integer );
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    Log.i( "MYAPP, "error - " + backendlessFault.getMessage() );
  }
} );
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "orderAmount > 100" );
Backendless.Data.of( Order.class ).getObjectCount( queryBuilder,
                                                   new AsyncCallback<Integer>()
{
  @Override
  public void handleResponse( Integer integer )
  {
    Log.i( "MYAPP", "found objects " + integer );
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    Log.i( "MYAPP, "error - " + backendlessFault.getMessage() );
  }
} );

The following sample request retrieves total number of related "child" objects for a parent object. The parent table is Person. It contains a relation column called address pointing to the Addresstable. The query below retrieves a count of related child objects for a parent object with objectID of XXXX-XXXX-XXXX-XXXX. The whereClause query syntax for this scenario is:

Person[address].objectId = 'XXXX-XXXX-XXXX-XXXX'

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "Person[address].objectId = 'XXXX-XXXX-XXXX-XXXX'" );
Backendless.Data.of( "Address" ).getObjectCount( queryBuilder,
                                                 new AsyncCallback<Integer>()
{
  @Override
  public void handleResponse( Integer integer )
  {
    Log.i( "MYAPP", "found objects " + integer );
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    Log.i( "MYAPP, "error - " + backendlessFault.getMessage() );
  }
} );
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "Person[address].objectId = 'XXXX-XXXX-XXXX-XXXX'" );
Backendless.Data.of( Address.class ).getObjectCount( queryBuilder,
                                                   new AsyncCallback<Integer>()
{
  @Override
  public void handleResponse( Integer integer )
  {
    Log.i( "MYAPP", "found objects " + integer );
  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    Log.i( "MYAPP, "error - " + backendlessFault.getMessage() );
  }
} );

Codeless Reference

data_service_get_objects_count

where:

Argument                Description
table name Name of the data table where to calculate the object count.
where clause Optional argument. If set, it is a search query used by the server it to determine the number of objects matching the condition. Refer to the Search With The Where Clause topic for more information.
distinct Used to return unique objects from the data table. Only custom properties/columns are considered in the query. Hence, if the data table contains a duplicate object, then it is not counted in the operation.

Returns the number of objects in a table (if the where clause is not set), otherwise the number of objects matching the search query.

Consider the following records in the employees data table:

data_service_example_get_objects_count_1

The example below counts specific objects in the data table since the where clause condition is set to exclude objects that contain the value 'Manager' in the column position. Thereby, objects with the 'Manager' values in the position column are not counted in the operation.

data_service_example_get_objects_count_2

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

data_service_example_bulk_update_4

Consider the data table below that contains two duplicate objects:

data_service_example_get_objects_count_3

The Codeless logic below has a where clause condition which is set to include in the operation only those objects that contain the value 'CEO' in the position column. The distinct parameter is set to true, hence the operation is set to count only unique objects, duplicates are skipped.

data_service_example_get_objects_count_5

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

data_service_example_get_objects_count_4