new Query(options) → {Object}
creates and returns a Query object that can be used in Collection methods or on its own to operate on items on the server
Parameters:
| Name | Type | Description |
|---|---|---|
options |
Object | Object that has configuration values used when instantiating a Query object |
- Source:
Returns:
Clearblade.Query the created query
- Type
- Object
Methods
equalTo(field, value)
Creates an equality clause in the query object
Parameters:
| Name | Type | Description |
|---|---|---|
field |
String | String defining what attribute to compare |
value |
String | String or Number that is used to compare against |
- Source:
Example
Adding an equality clause to a query
var query = ClearBlade.Query();
query.equalTo('name', 'John');
//will only match if an item has an attribute 'name' that is equal to 'John'
fetch(callback) → {ClearBlade.Item}
Reqests an item or a set of items from the query. Requires that
the Query object was initialized with a collection.
Parameters:
| Name | Type | Description |
|---|---|---|
callback |
function | Supplies processing for what to do with the data that is returned from the collection |
- Source:
Returns:
An array of ClearBlade Items
- Type
- ClearBlade.Item
Example
The typical callback
var query = ClearBlade.Query({'collection': 'COLLECTIONID'});
var callback = function (err, data) {
if (err) {
//error handling
} else {
console.log(data);
}
};
query.fetch(callback);
greaterThan(field, value)
Creates a greater than clause in the query object
Parameters:
| Name | Type | Description |
|---|---|---|
field |
String | String defining what attribute to compare |
value |
String | String or Number that is used to compare against |
- Source:
Example
Adding a greater than clause to a query
var query = ClearBlade.Query();
query.greaterThan('age', 21);
//will only match if an item has an attribute 'age' that is greater than 21
greaterThanEqualTo(field, value)
Creates a greater than or equality clause in the query object
Parameters:
| Name | Type | Description |
|---|---|---|
field |
String | String defining what attribute to compare |
value |
String | String or Number that is used to compare against |
- Source:
Example
Adding a greater than or equality clause to a query
var query = ClearBlade.Query();
query.greaterThanEqualTo('age', 21);
//will only match if an item has an attribute 'age' that is greater than or equal to 21
lessThan(field, value)
Creates a less than clause in the query object
Parameters:
| Name | Type | Description |
|---|---|---|
field |
String | String defining what attribute to compare |
value |
String | String or Number that is used to compare against |
- Source:
Example
Adding a less than clause to a query
var query = ClearBlade.Query();
query.lessThan('age', 50);
//will only match if an item has an attribute 'age' that is less than 50
lessThanEqualTo(field, value)
Creates a less than or equality clause in the query object
Parameters:
| Name | Type | Description |
|---|---|---|
field |
String | String defining what attribute to compare |
value |
String | String or Number that is used to compare against |
- Source:
Example
Adding a less than or equality clause to a query
var query = ClearBlade.Query();
query.lessThanEqualTo('age', 50);
//will only match if an item has an attribute 'age' that is less than or equal to 50
matches(field, pattern)
Creates an regular expression matching clause in the query object
Parameters:
| Name | Type | Description |
|---|---|---|
field |
String | String defining what attribute to compare |
pattern |
String | String or Number that is used to compare against |
- Source:
Example
Adding an regex matching clause to a query
var query = ClearBlade.Query();
query.matches('name', 'Smith$');
//will only match if an item has an attribute 'name' that That ends in 'Smith'
notEqualTo(field, value)
Creates a not equal clause in the query object
Parameters:
| Name | Type | Description |
|---|---|---|
field |
String | String defining what attribute to compare |
value |
String | String or Number that is used to compare against |
- Source:
Example
Adding a not equal clause to a query
var query = ClearBlade.Query();
query.notEqualTo('name', 'Jim');
//will only match if an item has an attribute 'name' that is not equal to 'Jim'
or(that)
chains an existing query object to the Query object in an or
Parameters:
| Name | Type | Description |
|---|---|---|
that |
Query | Query object that will be added in disjunction to this query object |
- Source:
Example
Chaining two queries together in an or
var query1 = ClearBlade.Query();
var query2 = ClearBlade.Query();
query1.equalTo('name', 'John');
query2.equalTo('name', 'Jim');
query1.or(query2);
//will match if an item has an attribute 'name' that is equal to 'John' or 'Jim'
remove(callback)
Removes an item or set of items from the Query
Parameters:
| Name | Type | Description |
|---|---|---|
callback |
function | Function that handles the response from the server |
- Source:
Example
Removing an item in a collection
//This example assumes that you have a collection with the item whose 'name' attribute is 'John'
var query = ClearBlade.Query({'collection': 'COLLECTIONID'});
query.equalTo('name', 'John');
var callback = function (err, data) {
if (err) {
throw new Error (data);
} else {
console.log(data);
}
};
query.remove(callback);
//removes every item whose 'name' attribute is equal to 'John'
setPage(pageSize, pageNum)
Set the pagination options for a Query.
Parameters:
| Name | Type | Description |
|---|---|---|
pageSize |
int | Number of items per response page. The default is 100. |
pageNum |
int | Page number, taking into account the page size. The default is 1. |
- Source:
update(changes, callback)
Updates an existing item or set of items. Requires that a collection was
set when the Query was initialized.
Parameters:
| Name | Type | Description |
|---|---|---|
changes |
Object | Object representing the attributes that you want changed |
callback |
function | Function that handles the response of the server |
- Source:
Example
Updating a set of items
//This example assumes a collection of items that have the columns name and age.
var query = ClearBlade.Query({'collection': 'COLLECTIONID'});
query.equalTo('name', 'John');
var changes = {
age: 23
};
var callback = function (err, data) {
if (err) {
throw new Error (data);
} else {
console.log(data);
}
};
query.update(changes, callback);
//sets John's age to 23