Javascript Queries

You probably have noticed that in our previous guide we used the

get method of the Simbla.Query to perform actions on a certain single object.  After reading the next guide, you will be able to use Simbla.Query to perform different actions on many objects at once and not just on one single object.

First, we will create an Simbla.Query and use find  In order to retrieve an Array of similar  Simbla.objects. In our next example we are going to use the  equalTo method to call back all the white cars.
var car = Simbla.Object.extend("Cars");
var query = new Simbla.Query(car);
query.equalTo("car_color", "white");
query.find({
  success: function(car) {
    // The object was retrieved successfully.
    var str="";
       for (var i=0;i         str += "

"+ results[i].get("car_model") + "

";
        str += "

"+ results[i].get("car_year") + "

";
        str += ""car_img").url() + "" style="width:200px" />";              
    }       
             
                 
 $("#snd").html(str);
   
  },
  error: function(car, error) {
    // The object was not retrieved successfully.
    alert("Error - The object was not retrieved successfully" + error.message);
  }
});

Query limitation 

Using the simbla.query can retrieve multiple objects. To filter the results you can endow several constraints.

Use the notEquelTo to filter out objects with a similar value :

query.notEqualTo("car_model", "tesla");

Adding multiple constraints is possible; they will act like they are connected with AND in between them, but only objects that match all constraints will be retrieved.

query.notEqualTo("car_model", "tesla");
query.greaterThan("car_year", 2015);

By default, the results are limited to 100, but if you wish to change it you can set a limit from 1 to 1000. By default, the results will be sorted by the first entered, meaning if you have 100 results and you limit them to 30, the first 30 will be presented and not the last 30.

query.limit(30);

If you wish to show the last 30 out of the 100, simply skip the first 70:

query.skip(70);

If you wish to retrieve only one result, use the first method:

var car = Simbla.Object.extend("Cars");
var query = new Simbla.Query(car);
query.equalTo("car_color", "white");
query.first({
  success: function(car) {
    // The object was retrieved successfully.
  },

  error: function(car, error) {
    // The object was not retrieved successfully.
    alert("Error - The object was not retrieved successfully" + error.message);
  }
});

Sort by

In order to sort the order of the retrieved objects(strings and numbers) use the following:

//  Ascending order by the chosen field
query.ascending("car_year"); 
// Descending order by the chosen field
query.descending("car_year");

Comparisons 

Comparisons can be used only on sortable objects(strings and numbers):

// Under the year 2015
query.lessThan("car_year", 2015);

// Under or equal to the year 2015
query.lessThanOrEqualTo("car_year", 2015);

// Above the year 2015
query.greaterThan("car_year", 2015);

// Above or equal to the year 2015
query.greaterThanOrEqualTo("car_year", 2015);

Instead of using multiple queries in order to retrieve an object out of a list of values, you can use the containedIn that provides an array of values.

For example, if you wish to find a car only from a specific list of car colors: 

// Find only from white , yellow or silver cars
query.containedIn("car_color",
                  ["white", "yellow", "silver"]);

You can use the notContainedIn to retrieve object that appear out of the list:

// Find cars that are neither white , yellow nor silver
query.notContainedIn("car_color",
                  ["white", "yellow", "silver"]);

Use exists to retrieve objects that have a particular key set, or use doesNotExist to retrieve objects that do not have a particular key set.

// Retrieve cars that have a car_year set:
query.exists("car_year");

// Retrieve cars that do not have a car_year set :
query.doesNotExist("car_year");

You can call back only certain fields. For this example, we can only retrieve "car code" and "car model" fields by using select :

query.select("car_code","car_model");

Use the startsWith  to retrieve string values that start with (" ").

For our example we will retrieve any car whose model starts with "Porsche;", in return we will get our "Porsche cayman" and "Porsche  911" but not any other values like: "porsche cayman" or "cayman Porsche".

query.startsWith("car_model", "Porsche");

Counting objects

If you wish to count how many objects match without retrieving them, use the count method. Below you will find a counter counting how many times "ford Shelby" appears in our table.

var Cars = Simbla.Object.extend("Cars");
var query = new Simbla.Query(Cars);
query.equalTo("car_model", "Ford Shelby");
query.count({
  success: function(count) {
    // Show count
    alert("Ford Shelby appears" + count + " times");
  },
  error: function(error) {
    // Count failed
  }
});

Compound Queries

If you want to find an object that matches several queries, you can use the Simbla.Query.or method. In our next example we are going to present the results of both old cars(under 2014) and new cars(above 2015) :

var newcar =  new Simbla.Query("Cars");
newcars.greaterThan("car_year", 2015)

var oldcars = Simbla.Object.extend("Cars");
oldcars.lessThan("car_year", 2014)

var mainQuery = Simbla.Query.or(newcars, oldcars);
mainQuery.find({
  success: function(results) {
    // The object was retrieved successfully.
    var str="";
     for (var i=0;i
        str += " + results[i].get("car_model") + "
        str += "+ results[i].get("car_year") + "
        str += ""car_img").url() + "" style="width:200px" />";              
    }
 $("#snd").html(str);
   
  },
  error: function(car, error) {
    // The object was not retrieved successfully.
    alert("Error - The object was not retrieved successfully" + error.message);
  }
});