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

var contactStorage = Backendless.Data.of( "Contact" );
var dataQuery = new Backendless.DataQuery();

// set where clause
dataQuery.condition = "age > 30";

// prepare options object (for relations, sorting, pageSize and offset)
dataQuery.options = {}

dataQuery.options.relations:[ "address","preferences" ] };

// request sorting
dataQuery.options.sortBy = [ "name" ]

// set offset and page size
dataQuery.options.pageSze = 20;
dataQuery.options.offset = 40;

contactStorage.find( dataQuery, new Backendless.Async(
  function( contactsCollection ) {
    console.log( "retrieved " + contactsCollection.data.length + " contacts" );
  }
));

Version 5.x

var contactStorage = Backendless.Data.of( "Contact" );
var queryBuilder = Backendless.DataQueryBuilder.create();

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

// request related objects for the columns
queryBuilder.setRelated( [ "address", "preferences" ] );

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

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

contactStorage.find( queryBuilder )
 .then( function( contactsArray ) {
    console.log( "retrieved " + contactsArray.length + " contacts" );
  });

Additional resources: