Filtering

Filtering

You can apply filters on any Create, Read, Update, and Delete data operations to alter the result of a query.

Retrieving by filter expression

You can read items by several filter expressions:

Value equal to

var el = new Everlive('your-app-id');

var filter = {
    'Author': 'Sample Text'
};
//or
var filter = new Everlive.Query();
filter.where().eq('Author', 'Sample Text');

var data = el.data('type-name');
data.get(filter)
    .then(function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });

Value not equal to

var el = new Everlive('your-app-id');
var data = el.data('type-name');
var query = new Everlive.Query();
query.where().ne('Author', 'Sample Text');
data.get(query) // filter
    .then(function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });

Value compared to

By executing the following code snippet, you can get all items which value is greater than a certain constant:

var el = new Everlive('your-app-id');
var data = el.data('type-name');
var query = new Everlive.Query();
query.where().gt('Author', 1);
data.get(query) // filter
    .then(function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });

You can also use the lt(), gte() and lte() operators to make correspondingly less-than, greater-than-or-equal-to and less-than-or-equal-to comparisons.

Value in array of possible matches

var el = new Everlive('your-app-id');
var data = el.data('type-name');
var query = new Everlive.Query();
query.where().isin('Author', ['Sample Text', 'Second Sample Text']);
data.get(query) // filter
    .then(function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });

You can also use the notin() method, which selects objects for which the specified field does not have any value in the specified array.

Boolean expressions

You can combine multiple filter expressions to get items. For example, to get items, which meet a list of criteria, you have to execute the following code snippet:

var el = new Everlive('your-app-id');
var data = el.data('type-name');
var query = new Everlive.Query();
query
    .where()
        .and()
            .eq('Author', 'Sample Text')
            .eq('Location', 'Second Sample Text')
    .done();
data.get(query) // filter
    .then(function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });

You can also use the not().or() and or() methods to get items which meet none or either of the conditions in the list.

Based on a date range

You can use the Date JavaScript object to specify the start and end dates for you query. The following example shows how to dynamic specify start and end dates relative to the current date:

var el = new Everlive('your-app-id');
var data = el.data('type-name');
var query = new Everlive.Query();

var rangeStart = new Date();
rangeStart.setDate(rangeStart.getDate() - 10); // 10 days before today
var rangeEnd = new Date();
rangeEnd.setDate(rangeEnd.getDate() + 10); // 10 days after today

query
    .where()
        .and()
            .gte('CreatedAt', rangeStart) // all items created after (>=) the rangeStart date
            .lte('CreatedAt', rangeEnd) // all items created before (<=) the rangeEnd date
        .done();

data.get(query) // filter
    .then(function(data) {
            alert(JSON.stringify(data));
        },
        function(error) {
            alert(JSON.stringify(error));
        });

Regular expressions

You can filter values that match a regex pattern. Telerik Platform understands Perl compatible regular expressions (PCRE), version 8.39, with UTF-8 support.

Regular expressions support when filtering data from Data Connectors is limited to the following use cases. As a workaround, create a view or a stored procedure and map it to a Telerik Platform content type or cloud function respectively.

  • Starts with: ^searchstring (Example: var filter = {"name":{"$regex":"^John"}};)
  • Ends with: searchstring$ (Example: var filter = {"name":{"$regex":"John$"}};)
  • Contains: .*searchstring.* (Example: var filter = {"name":{"$regex":".*John.*"}};)

You can specify regular expression options using the third argument of the whereQuery.regex() function.

Regular expression options are not supported when filtering data from Data Connectors. Do not specify any, otherwise you will receive an incorrect result.

var el = new Everlive('your-app-id');

var data = el.data('type-name');
var query = new Everlive.Query();

var startsWith = "^M";
// var endsWith = "v$";

query.where().regex('Author', startsWith, "i");
data.get(query) // filter
    .then(function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });

Complex expressions

You can build a complex filtering expression that combines several criteria using logical operators.

var query = new Everlive.Query();
query.where()
  .and()
   .or() // Name is eigher "John" or "Jane"
       .eq('FirstName', 'John')
       .eq('FirstName', 'Jane')
       .done()
   .and() // Age between 20 and 30
       .gt('Age', 20)
       .lt('Age', 30)
       .done()
   .done();

   // The result will be all people with Age between 20 and 30 and FirstName John or Jane:

Filtering an item inside an array

You can filter an array of primitive data types: number, string, or boolean:

//"ArrayField":["111222333","444555666","7778889999"]

var query = new Everlive.Query();
query.where().eq("ArrayField", "7778889999").done();

Deleting by filter expression

You can delete filtered items by executing the following code snippet:

var el = new Everlive('your-app-id');
var data = el.data('type-name');
var query = new Everlive.Query();
query.where().ne('Author', 'Sample Text');
data.destroy(query, // filter
    function (data) {
        alert('Items successfully deleted.');
    },
    function(error){
        alert(JSON.stringify(error));
    });

Updating by filter expression

To update filtered items, you have to executing the following code snippet:

var el = new Everlive('your-app-id');
var data = el.data('type-name');
var query = new Everlive.Query();
query.where().ne('Author', 'Sample Text');
data.update({ 'Author': 'Updated Sample Text' }, // data
    query, // filter
    function(data){
        alert(JSON.stringify(data));
    },
    function(error){
        alert(JSON.stringify(error));
    });
Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.