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
// Get total object count for the "TABLE-NAME" table
Backendless.Data.of( "TABLE-NAME" ).getObjectCount()
 .then( function( count ) {
  })
 .catch( function( error ) {
 });

// Get object count for the objects matching the 
// query from the "TABLE-NAME" table
Backendless.Data.of( "TABLE-NAME" ).getObjectCount( queryBuilder )
 .then( function( count ) {
  })
 .catch( function( error ) {
 });
// define constructor function. Instances of the DataTypeX class
// will be stored in the "DataTypeX" table
function DataTypeX() {
  // define class properties here
}

// Get total object count for the "DataTypeX" table
Backendless.Data.of( DataTypeX ).getObjectCount()
 .then( function( count ) {
  })
 .catch( function( error ) {
 });

// Get object count for the objects matching the query from 
// the "DataTypeX" table
Backendless.Data.of( DataTypeX ).getObjectCount( queryBuilder )
 .then( function( count ) {
  })
 .catch( function( error ) {
 });

where:

Argument                Description
DataTypeX name of the table where object count should be calculated.
"TABLE-NAME" name of the table where object count should be calculated.
queryBuilder instance of Backendless.DataQueryBuilder. With the "object count" API, the class can used only to set the "whereClause" as shown below:
DataQueryBuilder.create().setWhereClause( whereClauseValue );

Return Value

Returns the number of objects in the data table optionally matching the provided query.

Example

Total object count for a table:

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

Backendless.Data.of( "Order" ).getObjectCount()
 .then( function( count ) {
   console.log( "total objects in the Order table - " + count );
  })
 .catch( function( error ) {
   console.log( "error - " + error.message );
  });
function Order {
  // define the properties of the Order class here
}

Backendless.Data.of( Order ).getObjectCount()
 .then( function( count ) {
   console.log( "total objects in the Order table - " + count );
  })
 .catch( function( error ) {
   console.log( "error - " + error.message );
  });

Object count for a query:

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

var queryBuilder = DataQueryBuilder.create().setWhereClause( "orderAmount > 100" );

Backendless.Data.of( "Order" ).getObjectCount( queryBuilder )
 .then( function( count ) {
   console.log( "found objects matching query in the Order table - " + count );
  })
 .catch( function( error ) {
   console.log( "error - " + error.message );
  });
function Order {
  // properties of the Order class are defined here
}

var queryBuilder = DataQueryBuilder.create().setWhereClause( "orderAmount > 100" );

Backendless.Data.of( Order ).getObjectCount( queryBuilder )
 .then( function( count ) {
   console.log( "found objects matching query in the Order table - " + count );
  })
 .catch( function( error ) {
   console.log( "error - " + error.message );
  });

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'

Notice the API request is sent to Address (the child table):

var queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "Person[address].objectId = 'XXXX-XXXX-XXXX-XXXX'" );

Backendless.Data.of( "Address" ).getObjectCount( queryBuilder )
 .then( function( count ) {
   console.log( "found child objects for the parent - " + count );
  })
 .catch( function( error ) {
   console.log( "error - " + error.message );
  });
function Address {
  // properties of the Address class go here
}

var queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "Person[address].objectId = 'XXXX-XXXX-XXXX-XXXX'" );

Backendless.Data.of( Address ).getObjectCount( queryBuilder )
 .then( function( count ) {
   console.log( "found child objects for the parent " + count );
  })
 .catch( function( error ) {
   console.log( "error - " + error.message );
  });