Advanced Object Retrieval

Top  Previous  Next

Advanced search use-cases supported by Backendless include:

Search with a query (a "where clause" search) -retrieves data objects which satisfy a condition;
Paged data retrieval - retrieves a "page" of data of the given size from the specified offset.
Sorted data object retrieval - retrieves a collection of data objects sorted by specified properties.

Additionally, Backendless supports retrieval of related data objects.

Find objects within certain distance from a geo point

With the ability to link data objects with geo points, you can search for data objects by distance. This type of search returns all data objects which satisfy the whereClause condition and located within the specified distance. Distance-based search uses a special function in whereClause of the search request. The syntax of the function is:

   center point latitude, 
   center point longitude, 
   columnname which contains geo point.latitude,
   columnname which contains geo point.longitude )<operator> units-function(value)


<operator>- Possible values are <, >, =, >=, <=
units-function- Defines the units of measure for the distance. Possible values are:
ft( X ) - the distance value X is expressed in feet
km( X ) - the distance value X is expressed in kilometers
mi( X ) - the distance value X is expressed in miles
yd( X ) -  the distance value X is expressed in yards

For example, the following whereClause expression searches for data objects located within 200 miles from the point at 30.26715, -97.74306. Each data object must have the "coordinates" property of type GeoPoint.

distance( 30.26715, -97.74306, coordinates.latitude, coordinates.longitude ) < mi(200)

The following example demonstrates a search-by-distance query. The example uses three data objects stored in the Friend table: Bob, Jane, and Fred who respectively live in Austin, Houston, San Antonio. The search query in the example finds all friends who live within the specified distance. Before running the search query, create the objects in the data storage with the corresponding geo points.

The Friend class definition:

class Friend
    public string Name { get; set; }
    public String PhoneNumber { get; set; }
    public GeoPoint Coordinates { get; set; }

Run the following query/code to store a data object representing Bob with a link to his home in Austin, TX:

Friend bob = new Friend();
bob.Name = "Bob";
bob.PhoneNumber = "512-555-1212";
bob.Coordinates = new GeoPoint( 30.26715, -97.74306 );
bob.Coordinates.Categories.Add( "Home" );
bob.Coordinates.Metadata.Add( "description", "Bob's home" );
Backendless.Data.Of<Friend>().Save( bob );

Run the following query/code to store a data object representing Jane with a link to his home in Houston, TX:

Friend jane = new Friend();
jane.Name = "Jane";
jane.PhoneNumber = "281-555-1212";
jane.Coordinates = new GeoPoint( 29.76328, -95.36327 );
jane.Coordinates.Categories.Add( "Home" );
jane.Coordinates.Metadata.Add( "description", "Jane's home" );
Backendless.Data.Of<Friend>().Save( jane );

Run the following query/code to store a data object representing Fred with a link to his home in San Antonio, TX:

Friend fred = new Friend();
fred.Name = "Jane";
fred.PhoneNumber = "210-555-1212";
fred.Coordinates = new GeoPoint( 29.42412, -98.49363 );
fred.Coordinates.Categories.Add( "Home" );
fred.Coordinates.Metadata.Add( "description", "Fred's home" );
Backendless.Data.Of<Friend>().Save( fred );


Once the data is in the persistent object and geo location storage, run the following code/query to perform a distance-based search:

String whereClause = "distance( 30.26715, -97.74306, Coordinates.latitude, Coordinates.longitude ) < mi(200)";
BackendlessDataQuery dataQuery = new BackendlessDataQuery( whereClause );
dataQuery.QueryOptions = new QueryOptions()
  RelationsDepth = 1

BackendlessCollection<Friend> friends = Backendless.Data.Of<Friend>().Find( dataQuery );

foreach( Friend friend in friends.Data )
  System.Console.WriteLine( String.Format( "{0} lives at {1}, {2} tagged as '{3}'", friend.Name, friend.Coordinates.Latitude, friend.Coordinates.Longitude, friend.Coordinates.Metadata[ "description" ] ) );


The search returns all data objects within the specified distance. Each data object has the Coordinates property containing the coordinates of a geo point associated with this data object.



Please let us know how we can improve the documentation by leaving a comment. All technical questions should be posted to the Backendless Support forum. We do not respond to the technical questions on the documentation pages.: