SCQuery


init(collection)

Initialization of collection data query.

Properties

Parameter Type Properties Description Value example
collection String Mandatory Name of the collection where the object is added "items"

Example

var query = SCQuery(collection: "users")

.find(callback)

Method for searching objects based on the created sampling condition.

Properties

Parameter Type Properties Description Value example
callback (Bool, SCError?, [String: AnyObject]?) -> Void Callback for the request being executed.

Example

var query = SCQuery(collection: "users")
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.count(callback)

Method for counting objects that meet the query conditions.

Properties

Parameter Type Properties Description Value example
callback (Bool, SCError?, Int?) -> Void Callback for the request being executed.

Example

var query = SCQuery(collection: "users")
query.count() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.update(update, callback)

Method for updating the objects matching the sampling conditions.

Properties

Parameter Type Properties Description Value example
update SCUpdate Mandatory SCUpdate object to which data for updating is transferred
callback() (Bool, SCError?, [String: AnyObject]?) -> Void Callback for the request being executed.

Example

var userArrivalTime = SCUpdate()
let currentDate = SCUpdateOperator.currentDate("fieldName", typeSpec: "timestamp")
logArrivalTime.addOperator(currentDate)

var arrivedUsers = SCQuery(collection: "users")
arrivedUsers.equalTo("flightRace", SCString("AF4926"))
arrivedUsers.update(userArrivalTime) {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.remove(callback)

Method for removing the objects matching the sampling conditions.

Properties

Parameter Type Properties Description Value example
callback() (Bool, SCError?, [String: AnyObject]?) -> Void Callback for the request being executed.

Example

var oldStuff = SCQuery(collection: "Stuff")
oldStuff.lessThan("createdAt", SCDate("2016-06-54T17:24:23.091+03:00"))
oldStuff.remove() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.limit(limit)

Method for setting the sampling limit.

Properties

Parameter Type Properties Description Value example
limit Int Mandatory Sampling limit 100

Example

var query = SCQuery(collection: "items")
query.limit(25)
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.skip(skip)

Method for defining the number of documents to be skipped before document sampling

Properties

Parameter Type Properties Description Value example
limit Int Mandatory Number of skipped documents 1000

Example

var query = SCQuery(collection: "items")
query.skip(1000)
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.page(page)

Method for "per-page" output of sampling results in accordance with the specified sampling limit.

Properties

Parameter Type Properties Description Value example
page Int Mandatory Page number 4

Example

var query = SCQuery(collection: "items")
query.limit(25)
query.page(4)
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.raw(json)

Method for defining sampling conditions in the form of a JSON structure to create a DB query in MongoDB language.

Properties

Parameter Type Properties Description Value example
json String Mandatory Sampling conditions {location: {$in: ['New California Republic', 'Vault City']}}

Example

var query = SCQuery(collection: "items")
query.raw("{ \"fieldString\" : \"Строка\" }")
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.reset()

Method for resetting the sampling conditions

Example

var query = SCQuery(collection: "items")

query.equalTo("fieldName", SCString("John Doe"))
query.raw("{ \"fieldString\" : \"Строка\" }")
query.ascending("field1")
query.descending("field2")
query.fields(["field1", "field2"])
let and1 = SCOperator.EqualTo("fieldString", SCString("Строка"))
let and2 = SCOperator.EqualTo("fieldNumber", SCInt(33))
query.and([and1, and2])

query.reset()

.ascending(name)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Field name "price"

Example

var sortByPrice = SCQuery(collection: "items")
sortByPrice.ascending("price")
sortByPrice.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.descending(name)

Method for sorting data in the descending order of specified field values before sampling.

Properties

Parameter Type Properties Description Value example
name String Mandatory Field name "reward"

Example

var sortByReward = SCQuery(collection: "items")
sortByReward.descending("reward")
sortByReward.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.fields(names)

Method for specifying a list of returned fields.

Properties

Parameter Type Properties Description Value example
names [String] Mandatory Array of requested field values ["price", "reward"]

Example

var getPriceAndReward = SCQuery(collection: "items")
getPriceAndReward.fields(["price", "reward"])
getPriceAndReward.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.addOperator(name, oper)

Method for transferring a sampling condition to SCQuery

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field to which the condition is assigned "testcoll"
oper SCOperator Mandatory The assigned condition

Example

let lessNorEqual = SCOperator.LessThanOrEqualTo("price", 42)
SCQuery.addOperator(name, oper: lessNorEqual)

.equalTo(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field to which the condition is assigned "tags"
_ value SCValue Mandatory Field value 42

Example

var query = SCQuery(collection: "items")
query.equalTo("equality", SCString("yep"))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.notEqualTo(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field to which the condition is assigned "tags"
_ value SCValue Mandatory Field value 43

Example

var query = SCQuery(collection: "items")
query.notEqualTo("unequality", SCString("nope"))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.containedIn(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field to which the condition is assigned "price"
_ value SCArray Mandatory Array of values [-42, 41.999, 42]

Example

var query = SCQuery(collection: "items")
query.containedIn("someField", SCArray([SCString("A"), SCString("B")]))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.containsAll(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field to which the condition is assigned "strangeNumbers"
_ value SCArray Mandatory Array of values [4, 8, 15, 16, 23, 42]

Example

var query = SCQuery(collection: "items")
query.containsAll("someField", SCArray([SCString("A"), SCString("B")]))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.notContainedIn(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field to which the condition is assigned "tags"
_ value SCArray Mandatory Array of values 42

Example

var query = SCQuery(collection: "items")
query.notContainedIn("someField", SCArray([SCString("A"), SCString("B")]))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.greaterThan(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field to which the condition is assigned "reward"
_ value SCValue Mandatory Condition value 42

Example

var query = SCQuery(collection: "items")
query.greaterThan("reward", SCInt(100))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.greaterThanOrEqualTo(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field to which the condition is assigned "reward"
_ value SCValue Mandatory Condition value 42

Example

var query = SCQuery(collection: "items")
query.greaterThanOrEqualTo("createdAt", SCDate("2016-06-04T17:24:23.091+03:00"))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.lessThan(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field whose value should be updated "price"
_ value SCValue Mandatory Condition value 42

Example

var query = SCQuery(collection: "items")
query.lessThan("price", SCInt(42))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.lessThanOrEqualTo(name, _ value)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field whose value should be updated "price"
_ value SCValue Mandatory Condition value 42

Example

var query = SCQuery(collection: "items")
query.lessThanOrEqualTo("price", SCInt(42))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.exists(name)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field for which a condition is defined "price"

Example

var query = SCQuery(collection: "items")
query.exists("reward")
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.doesNotExist(name)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field for which a condition is defined "price"

Example

var query = SCQuery(collection: "items")
query.doesNotExist("price")
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.contains(name, _ pattern)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field for which a condition is defined "stringsWithNumbers"
_ pattern String Mandatory Regular expression [0-9]

Example

var query = SCQuery(collection: "items")
query.contains("description", "[a-zA-Z0-9]")
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.startsWith(name, _ pattern)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field for which a condition is defined "labels"
_ pattern String Mandatory Condition value "neverendi"

Example

var query = SCQuery(collection: "items")
query.startsWith("fieldString", "[A-Z]")
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.endsWith(name, _ pattern)

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

Properties

Parameter Type Properties Description Value example
name String Mandatory Name of the field whose value should be updated "labels"
_ pattern String Mandatory Condition value "ngdocuments"

Example

var query = SCQuery(collection: "items")
query.endsWith("fieldString", "ing")
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.and(operators)

Method for logical multiplication of conditions of several samplings

Properties

Parameter Type Properties Description Value example
operators [SCOperator] Mandatory Sampling condition which is included in the conjunction

Example

var query = SCQuery(collection: "items")
query.notEqualTo("unequality", SCString("nope"))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}

.or(operators)

Method for Boolean addition of conditions of several samplings

Properties

Parameter Type Properties Description Value example
operators [SCOperator] Mandatory Sampling condition which is included in the disjunction

Example

var query = SCQuery(collection: "items")
query.notEqualTo("unequality", SCString("nope"))
query.find() {
    success, error, result in
    if success {
        print("Success")
    } else {
        if let error = error {
            print("Error")
        }
    }
}