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
Backendless.Data.Of( "TABLE-NAME" ).GetObjectCount( AsyncCallback<int> callback );

// get object count for all objects in the table which match the query
Backendless.Data.Of( "TABLE-NAME" ).GetObjectCount( 
                                                   DataQueryBuilder queryBuilder,
                                                   AsyncCallback<int> callback );
// get object count for all objects in the table
Backendless.Data.Of( E ).GetObjectCount( AsyncCallback<int> callback );

// get object count for all objects in the table which match the query
Backendless.Data.Of( E ).GetObjectCount( DataQueryBuilder queryBuilder,
                                         AsyncCallback<int> callback );

Blocking API

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

// get object count for all objects in the table which match the query
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 a .NET class which identifies the table where to calculate the object count.
queryBuilder Instance of BackendlessAPI.Persistence.DataQueryBuilder. When present in the arguments, must contain a whereClause query. The query is used by the server to identify a collection of objects to calculate the count of.
callback a callback object which will receive a callback when the method successfully calculates the object count. Applies to the non-blocking methods only.

Return Value

The blocking methods returns an integer value which is the object count. Non-blocking methods receive a callback with the count value.

Example

Total object count for a table:

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

AsyncCallback<int> getCountCallback = new AsyncCallback<int>(
  objectCount => 
    {
      System.Console.WriteLine( "object count is " + objectCount );
    },
  error => 
    {
      System.Console.WriteLine( "Server returned an error " + error.Message );
    }
  );
Backendless.Data.Of( "Order" ).GetObjectCount( getCountCallback );
AsyncCallback<int> getCountCallback = new AsyncCallback<int>(
  objectCount => 
    {
      System.Console.WriteLine( "object count is " + objectCount );
    },
  error => 
    {
      System.Console.WriteLine( "Server returned an error " + error.Message );
    }
  );
Backendless.Data.Of<Order>().GetObjectCount( getCountCallback );

Object count for a query:

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

AsyncCallback<int> getCountCallback = new AsyncCallback<int>(
  objectCount => 
    {
      System.Console.WriteLine( "object count is " + objectCount );
    },
  error => 
    {
      System.Console.WriteLine( "Server returned an error " + error.Message );
    }
  );

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.SetWhereClause( "orderAmount > 100" );
Backendless.Data.Of( "Order" ).GetObjectCount( queryBuilder, getCountCallback );
AsyncCallback<int> getCountCallback = new AsyncCallback<int>(
  objectCount => 
    {
      System.Console.WriteLine( "object count is " + objectCount );
    },
  error => 
    {
      System.Console.WriteLine( "Server returned an error " + error.Message );
    }
  );

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.SetWhereClause( "orderAmount > 100" );
Backendless.Data.Of<Order>().GetObjectCount( queryBuilder, getCountCallback );

Related object count:

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'

AsyncCallback<int> getCountCallback = new AsyncCallback<int>(
  objectCount => 
    {
      System.Console.WriteLine( "object count is " + objectCount );
    },
  error => 
    {
      System.Console.WriteLine( "Server returned an error " + error.Message );
    }
  );

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.SetWhereClause( "Person[address].objectId = 'XXXX-XXXX-XXXX-XXXX'" );
Backendless.Data.Of( "Address" ).GetObjectCount( queryBuilder,
                                                 getCountCallback );
AsyncCallback<int> getCountCallback = new AsyncCallback<int>(
  objectCount => 
    {
      System.Console.WriteLine( "object count is " + objectCount );
    },
  error => 
    {
      System.Console.WriteLine( "Server returned an error " + error.Message );
    }
  );

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.SetWhereClause( "Person[address].objectId = 'XXXX-XXXX-XXXX-XXXX'" );
Backendless.Data.Of<Address>().GetObjectCount( queryBuilder,
                                                  getCountCallback );