# Class: Query

## ClearBlade. Query

#### 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:
    - ClearBlade.js, line 975

  ##### Returns:

  Clearblade.Query the created query
  Type
    Object

### Methods

#### columns(Columns, callback)

  Gets rows of the specified columns

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `Columns` | Object | object
  `callback` | function | Function that handles the response of the server

  Source:
    - ClearBlade.js, line 1275

  ##### Example

  Getting values of columns

      //This example assumes a collection of items that have the columns name and age.
      var query = ClearBlade.Query({'collectionName': 'COLLECTIONNAME'});
      var callback = function (err, data) {
         if (err) {
             throw new Error (data);
         } else {
             console.log(data);
         }
      };

      query.columns(["name","age"]);
      query.fetch(callback);
      //gets values in columns name and age

#### 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:
    - ClearBlade.js, line 1049

  ##### 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:
    - ClearBlade.js, line 1195

  ##### 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:
    - ClearBlade.js, line 1064

  ##### 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:
    - ClearBlade.js, line 1079

  ##### 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:
    - ClearBlade.js, line 1094

  ##### 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:
    - ClearBlade.js, line 1109

  ##### 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:
    - ClearBlade.js, line 1139

  ##### 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:
    - ClearBlade.js, line 1124

  ##### 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:
    - ClearBlade.js, line 1154

  ##### 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:
    - ClearBlade.js, line 1302

  ##### 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:
    - ClearBlade.js, line 1181

#### 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:
    - ClearBlade.js, line 1235

  ##### 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