Skip to content

Data Query

Data service find operation can accept an argument with additional options instructing the server to sort data, retrieve paged results and return related child objects. The example below demonstrates the usage of the data query argument in versions 3.x and 5.x:

Version 3.x

IDataStore<Map> contactStorage = Backendless.Data.of( "Contact" );
BackendlessDataQuery dataQuery = new BackendlessDataQuery();

// set where clause
dataQuery.setWhereClause( "age > 30" );

QueryOptions queryOptions = new QueryOptions();
// set related columns
queryOptions.addRelated( "address" );
queryOptions.addRelated( "preferences" );

// request sorting
List<String> sortBy = new ArrayList<String>();
sortBy.add( "name");
queryOptions.setSortBy( sortBy );

// set offset and page size
queryOptions.setPageSize( 20 );
queryOptions.setOffset( 40 );

// set query options into data query
dataQuery.setQueryOptions( queryOptions );

contactStorage.find( dataQuery, new AsyncCallback<BackendlessCollection<Map>>()
{
  @Override
  public void handleResponse( BackendlessCollection<Map> contactObjects )
  {
     Log.i( "MYAPP", "Retrieved " + contactObjects.getCurrentPage().size() + " objects" );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
     Log.e( "MYAPP", "Server reported an error " + fault.getMessage() );
  }
});

Version 5.x

IDataStore<Map> contactStorage = Backendless.Data.of( "Contact" );
DataQueryBuilder queryBuilder = DataQueryBuilder.create();

// set where clause
queryBuilder.setWhereClause( "age > 30" );

// set related columns
queryBuilder.setRelated( "address", "preferences" );

// request sorting
queryBuilder.setSortBy( "name" );

// set offset and page size
queryBuilder.setPageSize( 20 );
queryBuilder.setOffset( 40 );

contactStorage.find( queryBuilder, new AsyncCallback<List>()
{
  @Override
  public void handleResponse( List<Map> contactObjects )
  {
    Log.i( "MYAPP", "Retrieved " + contactObjects.size() + " objects" );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "Server reported an error " + fault.getMessage );
  }
} );

Additional resources: