Skip to content

Inverted Relation Retrieval

Backendless supports a special query syntax for loading a subset of child objects for a specific parent. Consider the following table schemas:

PhoneBook table:

phonebook-table-schema

Contact table:

contact-table-schema

Address table:

address-table-schema

These tables will be used to demonstrate how to query Backendless for conditional related object retrieval.

The untyped objects approach does not require you defining classes for the objects stored in Backendless. Instead your code can use plain JS objects to to store and retrieve objects in/from Backendless.
Consider the following class definitions for the entities from the diagram: PhoneBook class:
function PhoneBook( args ) {
    args = args || {};
    this.___class = 'PhoneBook';
    this.owner = args.owner || {}; // Contact
    this.contacts = args.contacts || null; // collection of Contacts
}
Contact class:
function Contact(args) {
    args = args || {};
    this.___class = 'Contact';
    this.name = args.name || "";
    this.age = args.age || "";
    this.phone = args.phone || "";
    this.title = args.title || "";
    this.address = args.address || {};
}
Address class:
function Address(args) {
    args = args || {};
    this.___class = 'Address';
    this.street = args.street || "";
    this.city = args.city || "";
    this.state = args.state || "";
}

The general structure of a whereClause query to load a collection of child objects for a specific parent object is:

ParentTableName[ relatedColumnName ].parentColumnName COLUMN-VALUE-CONDITION

Both columns relatedColumnName and parentColumnName must be declared in a table with name of ParentTableName. The relatedColumnName must be a relation column. The table relatedColumnName points to is the table where the objects must be loaded from.  The examples below demonstrate the usage of this syntax:

Find all contacts in a city for a specific phone book

// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that can be done
var contactStorage = Backendless.Data.of( "Contact" );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                        JohnsPhoneBookID + 
                        "' and address.city='Denver'" );
var contactsFromDenver = contactStorage.find( queryBuilder );
// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that can be done
var contactStorage = Backendless.Data.of( Contact );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                        JohnsPhoneBookID + 
                        "' and address.city='Denver'" );
var contactsFromDenver = contactStorage.find( queryBuilder );

Find all contacts for the specific phone book where the city name contains letter 'a'

// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that 
// can be doneuse any of the samples above

var contactStorage = Backendless.Persistence.of( "Contact" );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                         JohnsPhoneBookID + 
                         "' and address.city like '%a%'" );
var contactsFromDenver = contactStorage.find( queryBuilder );
// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that 
// can be doneuse any of the samples above

var contactStorage = Backendless.Persistence.of( Contact );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                         JohnsPhoneBookID + 
                         "' and address.city like '%a%'" );
var contactsFromDenver = contactStorage.find( queryBuilder );

Find all contacts where age is greater than 20 for a specific phone book

// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that 
// can be doneuse any of the samples above

var contactStorage = Backendless.Persistence.of( "Contact" );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                         JohnsPhoneBookID + 
                         "' and age > 20" );
var contactsFromDenver = contactStorage.find( queryBuilder );
// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that 
// can be doneuse any of the samples above

var contactStorage = Backendless.Persistence.of( Contact );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                         JohnsPhoneBookID + 
                         "' and age > 20" );
var contactsFromDenver = contactStorage.find( queryBuilder );

Find all contacts for a specific phone book where age is within the specified range

// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that 
// can be doneuse any of the samples above

var contactStorage = Backendless.Persistence.of( "Contact" );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                         JohnsPhoneBookID + 
                         "' and age >= 21 and age <= 30" );
var contactsFromDenver = contactStorage.find( queryBuilder );
// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that 
// can be doneuse any of the samples above

var contactStorage = Backendless.Persistence.of( Contact );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                         JohnsPhoneBookID + 
                         "' and age >= 21 and age <= 30" );
var contactsFromDenver = contactStorage.find( queryBuilder );

Find all contacts for a specific phone book where age is greater than 20 and the city is Tokyo

// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that 
// can be doneuse any of the samples above

var contactStorage = Backendless.Persistence.of( "Contact" );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                        JohnsPhoneBookID + 
                        "' and age > 20 and address.city = 'Tokyo'" );
var contactsFromDenver = contactStorage.find( queryBuilder );
// assume a phone book is created with one or more contacts. 
// See the relevant samples above which demonstrate how that 
// can be doneuse any of the samples above

var contactStorage = Backendless.Persistence.of( Contact );
var JohnsPhoneBookID = JohnsPhoneBook[ "objectId" ].split( '.' )[0];
var queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "PhoneBook[contacts].objectId='" + 
                        JohnsPhoneBookID + 
                        "' and age > 20 and address.city = 'Tokyo'" );
var contactsFromDenver = contactStorage.find( queryBuilder );