Scorocode.Query


new Query(collName)

Instance of a collection data query

Parameter Type Description
collName String Collection name

Example

var sc = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var data = new Scorocode.Query("items");
data.find()
    .then((finded) =>{
        console.log(finded);
    })
    .catch((err)=>{
        console.log(err)
    });    
Returns: Scorocode.Query - Returns the Scorocode.Query

Exception:

  • String 'Collection name must be a type of string'

.find(options)

Method for requesting a document from a collection. Returns data of the objects that match the sampling criteria. If no criteria are set, the first 50 objects of the collection are returned by default.

Parameter Type Description
options Object Success and error callbacks for the executed query.

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var data = new Scorocode.Query("items");
data.find()
    .then((finded) =>{
        var util = require('util');
        console.log(util.inspect(finded, {showHidden: false, depth: null}))
    })
    .catch((err)=>{
        console.log(err)
    });    

Returns: promise.{error: Boolean, limit: Number, skip: Number, result: [{Scorocode.Object}]} - Returns promise, which returns the object containing the result of the query execution.

  • "error" - Boolean - Error flag
  • "limit" - Number - Sampling size limit
  • "skip" - Number - How many documents were skipped during the sampling
  • "result" - Array - Obtained data array
{ 
    error: false,
    limit: 100,
    skip: 0,
    result:
    [ 
       { _id: 'CrT49joIxn',
           createdAt: Wed May 25 2016 17:24:17 GMT+0300 (RTZ 2 (зима)),
           updatedAt: Wed May 25 2016 22:15:03 GMT+0300 (RTZ 2 (зима)),
           readACL: [],
           updateACL: [],
           removeACL: [],
           arrayField: [ false,"",42.42,[1,2,3],["Array",{"123": 4}],{ "Object": true }],
           price: 41.999 
       },
       // ...
       { _id: 'NseSaqqd5v',
           createdAt: Wed May 25 2016 17:24:17 GMT+0300 (RTZ 2 (зима)),
           updatedAt: Wed May 25 2016 22:15:03 GMT+0300 (RTZ 2 (зима)),
           readACL: [],
           updateACL: [],
           removeACL: []
       } 
    ]
}

.count(options)

Method for counting objects that meet the query conditions.

Parameter Type Description
options Object Success and error callbacks for the executed query.

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var countItems = new Scorocode.Query("items");
countItems.exists("price")
    .count()
        .then((counted) => {
            console.log(counted) // { error: false, result: 5 }
        })
        .catch((error) => {
            console.log(error)
        });

Returns: promise.{error: Boolean, result: Number} - Returns promise, which returns the object containing the result of the query execution.

  • "error" - Boolean - Error flag
  • "result" - Number - Number of objects that meet the sampling condition.

.update(Object, options)

Method for updating the requested objects.

Parameter Type Description
Object Scorocode.UpdateOps Scorocode.UpdateOps object to which the updated data is transferred.
options Object Success and error callbacks for the executed query.

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");

Items.notEqualTo("price", 42)
    .find()
        .then((result) => {
            updateItems.set("price", 42);
            return Items.update(updateItems)
        })
        .then((updated) => {
            console.log(updated);
        }) 
        .catch((error) => {
            console.log(error)
        });

Returns: promise.{error: Boolean, result: {count: Number, docs: Array}} - Returns promise, which returns the object containing the result of the query execution.

  • "error" - Boolean - Error flag
  • "result" - Object - Query execution result
    • "count" - Number - Number of modified objects
    • "docs" - Array - _id array of modified objects
{ error: false,
  result:
   { count: 8,
     docs:[ 
        'CrT49joIxn',
        '8Qcfll2GwE',
        'dMSYsK8jld',
        '6TFVG5UqV6',
        'gNxzwAfvDj',
        'eoVWeg9oeY',
        'vRf58kEDpo',
        'abOkjQAnYE' 
        ] 
    } 
}

.remove(options)

Method for removing the requested objects.

Parameter Type Description
options Object Success and error callbacks for the executed query.

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

Items.exists("arrayField")
    .find()
        .then((finded) => {
            Items.remove(finded)
                .then((result) => {
                    console.log(result);
                })  
                .catch((error) => {
                    console.log(error)
                });
        })
        .catch((error) => {
                console.log(error)
        });

Returns: promise.{ecount: Number, docs: Array} - Returns promise, which returns the object containing the result of the query execution.

  • "count" - Number - Number of deleted objects
  • "docs" - Array - _id array of deleted objects
{ 
    count: 4, 
    docs:[ 
        'CrT49joIxn', 
        'eoVWeg9oeY', 
        'vRf58kEDpo', 
        'abOkjQAnYE'
        ] 
}

.reset()

Method for resetting the sampling conditions

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.equalTo("price", 42)
    .find()
        .then((result) => {
            console.log(result)
        })
        .catch((error) => {
            getItems.reset()
            console.log(error)
        });

.equalTo(field, value)

Method for retrieving all objects with the field value indicated in the condition.

Parameter Type Description
field String Name of the field for which a condition is defined
value String / Number / Boolean / Date / Array / Object Field value

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.equalTo("price", 42)
    .find()
        .then((result) => {
            console.log(result) 
            getItems.reset()
        })
        .catch((error) => {
            console.log(error)
        });

.notEqualTo(field, value)

Method for retrieving all objects except for objects with the field value indicated in the condition.

Parameter Type Description
field String Name of the field for which a condition is defined
value String / Number / Boolean / Date / Array / Object Field value

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.notEqualTo("price", 42)
    .find()
        .then((result) => {
            console.log(result) // { error: false, result: 5 }
        })
        .catch((error) => {
            console.log(error)
        });

.containedIn(field, value)

Method for retrieving all objects whose field value contains the array elements specified in the query.

Parameter Type Description
field String Name of the field for which a condition is defined
value Array Array of values

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.containedIn("price",[-42, 41.999, 42])
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exceptions

  • String 'Value must be of Тип: Array'

.containsAll(field, value)

Method for retrieving all objects whose field value contains all array elements specified in the query.

Parameter Type Description
field String Name of the field for which a condition is defined
value Array Array of values
var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.containsAll("arrayField",[4, 8, 15, 16, 23, 42])
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exceptions:

  • String 'Value must be of Тип: Array'

.notContainedIn(field, value)

Method for retrieving all objects whose field value does not contain the array elements specified in the query.

Parameter Type Description
field String Name of the field for which a condition is defined
value Array Array of values

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.notContainedIn("price",[41.999, 42])
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exceptions:

  • String 'Value must be of Тип: Array'

.greaterThan(field, value)

Method for retrieving all objects whose field value is greater than the number specified in the query.

Parameter Type Description
field String Name of the field for which a condition is defined
value Number / Date Condition value

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.greaterThan("createdAt", "2016-05-19T15:35:16.000Z")
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

.greaterThanOrEqualTo(field, value)

Method for retrieving all objects whose field value is no less than the number specified in the query.

Parameter Type Description
field String Name of the field for which a condition is defined
value Number / Date Condition value

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.greaterThanOrEqualTo("price", 41.999)
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

.lessThan(field, value)

Method for retrieving all objects whose field value is less than the number specified in the query.

Parameter Type Description
field String Name of the field for which a condition is defined
value Number / Date Condition value

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.lessThan("price", 41)
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

.lessThanOrEqualTo(field, value)

Method for retrieving all objects whose field value is no greater than the number specified in the query.

Parameter Type Description
field String Name of the field for which a condition is defined
value Number / Date Condition value

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.lessThanOrEqualTo("updatedAt", "2016-05-19T15:35:16.000Z")
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

.exists(field)

Method for retrieving all objects with an existing value of a defined field

Parameter Type Description
field String Name of the field for which a condition is defined

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var Items = new Scorocode.Query("items");
Items.exists("price")
    .find()
        .then((result) => {
            console.log(result)
        .catch((error) => {
            console.log(error)
        });

.doesNotExist(field)

Method for retrieving all objects with a missing value in a defined field.

Parameter Type Description
field String Name of the field for which a condition is defined

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var Items = new Scorocode.Query("items");
Items.doesNotExist("price")
    .find()
        .then((result) => {
            console.log(result)
        .catch((error) => {
            console.log(error)
        });

.contains(field, value)

Method for retrieving all objects with a value of a defined field that matches a defined regular expression.

Parameter Type Description
field String Name of the field for which a condition is defined
value String Regular expression

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.contains("someString","[0-9]")
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exception:

  • String '"Value must be a string"'

.startsWith(field, value)

Method for retrieving all objects with a value of a defined field starting from a specified string.

Parameter Type Description
field String Name of the field for which a condition is defined
value String Condition value

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.startsWith("name", "ite");
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exception:

  • String "Value must be a string"

.endsWith(field, value)

Method for retrieving all objects with a value of a defined field ending with a specified string.

Parameter Type Description
field String Field name
value String Condition value
var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.endsWith("name", "waterchip");
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exception:

  • String "Value must be a string"

.limit(limit)

Method for specifying a limit for the number of sampling objects

Parameter Type Description
limit Number Sampling limit

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.limit(1000).contains("someString","[a-zA-Z-0-9]")
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exception:

  • String "Limit must be a positive number"

.skip(skip)

Method for skipping some objects before sampling.

Parameter Type Description
skip Number Number of skipped objects

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.limit(1000).skip(1000).contains("someString","[a-zA-Z-0-9]")
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exception

  • String "Skip must be a positive number"

.page(page)

Method for sampling results page by page

Parameter Type Description
page Number Page number

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.limit(30).page(2).contains("someString","[a-zA-Z-0-9]")
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exception:

  • String "Page must be a positive number"

.ascending(field)

Method for sorting a specified field data in ascending order before sampling.

Parameter Type Description
field String Field name

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.limit(30).ascending("updatedAt").page(1).contains("someString","[a-zA-Z-0-9]")
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

.descending(field)

Method for sorting a specified field data in descending order before sampling.

Parameter Type Description
field String Field name

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
getItems.limit(30).descending("price").page(1).contains("someString","[a-zA-Z-0-9]")
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

.or(query)

Method for logical addition of several samplings conditions

Parameter Type Description
query Scorocode.Query Query that is included in the disjunction operation

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
var range1 = new Scorocode.Query("items");
var range2 = new Scorocode.Query("items");

var getItems = new Scorocode.Query("items");
range1.lessThanOrEqualTo("createdAt", "2016-05-19T10:00:00.000Z");
range2.greaterThanOrEqualTo("createdAt", "2016-05-21T15:00:00.000Z");
getItems.or(range1).or(range2)
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

Exception:

  • String "Invalid type of Query"

.and(query)

Method for logical multiplication of several samplings conditions

Parameter Type Description
query Scorocode.Query Query that is included in the conjunction operation

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var getItems = new Scorocode.Query("items");
var range = new Scorocode.Query("items");
var price = new Scorocode.Query("items");

var getItems = new Scorocode.Query("items");
range.greaterThanOrEqualTo("createdAt", "2016-05-19T10:00:00.000Z");
price.doesNotExists("price");
getItems.and(range).and(price)
    .find()
        .then((result) => {
            console.log(result) 
        })
        .catch((error) => {
            console.log(error)
        });

.select()

Method for specifying a list of returned fields.

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var data = new Scorocode.Query("items");
data.select("price", "reward").find()
    .then((finded) =>{
        console.log(finded);
    })
    .catch((err)=>{
        console.log(err)
    });    

.raw(filter)

Parameter Type Description
filter Object Applied filter in the MongoDB query language format

Example

var Scorocode = require('scorocode');
Scorocode.Init({
    ApplicationID: "applicationId",
    JavaScriptKey: "javascriptKey"
});

var query = Scorocode.Query("items");
query.raw("{ \"fieldString\" : \"String\" }");
query.find()
    .then((finded) =>{
        console.log(finded);
    })
    .catch((err)=>{
        console.log(err)
    });