Class: Collection

Collection(name, optionsopt)

new Collection(name, optionsopt)

Collection class that handles documents of same type

Parameters:
Name Type Attributes Description
name string

collection name

options array | object <optional>

(optional) array of property names to be indicized OR a configuration object

Properties
Name Type Attributes Default Description
unique array <optional>
[]

array of property names to define unique constraints for

exact array <optional>
[]

array of property names to define exact constraints for

indices array <optional>
[]

array property names to define binary indexes for

adaptiveBinaryIndices boolean <optional>
true

collection indices will be actively rebuilt rather than lazily

asyncListeners boolean <optional>
false

whether listeners are invoked asynchronously

disableChangesApi boolean <optional>
true

set to false to enable Changes API

disableDeltaChangesApi boolean <optional>
true

set to false to enable Delta Changes API (requires Changes API, forces cloning)

autoupdate boolean <optional>
false

use Object.observe to update objects automatically

clone boolean <optional>
false

specify whether inserts and queries clone to/from user

serializableIndices boolean <optional>
true[]

converts date values on binary indexed properties to epoch time

cloneMethod string <optional>
'parse-stringify'

'parse-stringify', 'jquery-extend-deep', 'shallow', 'shallow-assign'

ttlInterval int

time interval for clearing out 'aged' documents; not set by default.

Implements:
Source:
See:

Methods

addDynamicView(name, optionsopt) → {DynamicView}

Add a dynamic view to the collection

Parameters:
Name Type Attributes Description
name string

name of dynamic view to add

options object <optional>

options to configure dynamic view with

Properties
Name Type Attributes Default Description
persistent boolean <optional>
false

indicates if view is to main internal results array in 'resultdata'

sortPriority string <optional>
'passive'

'passive' (sorts performed on call to data) or 'active' (after updates)

minRebuildInterval number

minimum rebuild interval (need clarification to docs here)

Source:
Returns:

reference to the dynamic view added

Type
DynamicView
Example
var pview = users.addDynamicView('progeny');
pview.applyFind({'age': {'$lte': 40}});
pview.applySimpleSort('name');

var results = pview.data();

addTransform(name, transform)

Adds a named collection transform to the collection

Parameters:
Name Type Description
name string

name to associate with transform

transform array

an array of transformation 'step' objects to save into the collection

Source:
Example
users.addTransform('progeny', [
  {
    type: 'find',
    value: {
      'age': {'$lte': 40}
    }
  }
]);

var results = users.chain('progeny').data();

avg(field) → {number}

Calculates the average numerical value of a property

Parameters:
Name Type Description
field string

name of property in docs to average

Source:
Returns:

average of property in all docs in the collection

Type
number

by(field, value) → {object}

Retrieve doc by Unique index

Parameters:
Name Type Description
field string

name of uniquely indexed property to use when doing lookup

value value

unique value to search for

Source:
Returns:

document matching the value passed

Type
object

chain(transformopt, parametersopt) → {Resultset}

Chain method, used for beginning a series of chained find() and/or view() operations on a collection.

Parameters:
Name Type Attributes Description
transform array <optional>

Ordered array of transform step objects similar to chain

parameters object <optional>

Object containing properties representing parameters to substitute

Source:
Returns:

(this) resultset, or data array if any map or join functions where called

Type
Resultset

clear(optionsopt)

Empties the collection.

Parameters:
Name Type Attributes Description
options object <optional>

configure clear behavior

Properties
Name Type Attributes Description
removeIndices bool <optional>

(default: false)

Source:

commitStage(stageName, message)

(Staging API) re-attach all objects to the original collection, so indexes and views can be rebuilt then create a message to be inserted in the commitlog

Parameters:
Name Type Description
stageName string

name of stage

message string
Source:

configureOptions()

Will allow reconfiguring certain collection options.

Parameters:
Name Type Description
options.adaptiveBinaryIndices boolean

collection indices will be actively rebuilt rather than lazily

Source:

count(queryopt) → {number}

Quickly determine number of documents in collection (or query)

Parameters:
Name Type Attributes Description
query object <optional>

(optional) query object to count results of

Source:
Returns:

number of documents in the collection

Type
number

ensureIndex(property, forceopt)

Ensure binary index on a certain field

Parameters:
Name Type Attributes Description
property string

name of property to create binary index on

force boolean <optional>

(Optional) flag indicating whether to construct index immediately

Source:

eqJoin(joinData, leftJoinProp, rightJoinProp, mapFunopt, dataOptionsopt) → {Resultset}

Join two collections on specified properties

Parameters:
Name Type Attributes Description
joinData array | Resultset | Collection

array of documents to 'join' to this collection

leftJoinProp string

property name in collection

rightJoinProp string

property name in joinData

mapFun function <optional>

(Optional) map function to use

dataOptions object <optional>

options to data() before input to your map function

Properties
Name Type Description
removeMeta bool

allows removing meta before calling mapFun

forceClones boolean

forcing the return of cloned objects to your map object

forceCloneMethod string

Allows overriding the default or collection specified cloning method.

Source:
Returns:

Result of the mapping operation

Type
Resultset

extract()

Source:

extractNumerical()

Source:

find(query) → {array}

Find method, api is similar to mongodb. for more complex queries use chain() or where().

Parameters:
Name Type Description
query object

'mongo-like' query object

Source:
Returns:

Array of matching documents

Type
array
Example
Query Examples

findAndRemove(filterObject)

Applies a 'mongo-like' find query object removes all documents which match that filter.

Parameters:
Name Type Description
filterObject object

'mongo-like' query object

Source:

findAndUpdate(filterObject, updateFunction)

Applies a 'mongo-like' find query object and passes all results to an update function. For filter function querying you should migrate to updateWhere().

Parameters:
Name Type Description
filterObject object | function

'mongo-like' query object (or deprecated filterFunction mode)

updateFunction function

update function to run against filtered documents

Source:

findOne(query) → {object|null}

Find one object by index property, by property equal to value

Parameters:
Name Type Description
query object

query object used to perform search with

Source:
Returns:

First matching document, or null if none

Type
object | null

get(id, returnPosition) → {object|array|null}

Get by Id - faster than other methods because of the searching algorithm

Parameters:
Name Type Description
id int

$loki id of document you want to retrieve

returnPosition boolean

if 'true' we will return [object, position]

Source:
Returns:

Object reference if document was found, null if not, or an array if 'returnPosition' was passed.

Type
object | array | null

getDynamicView(name) → {DynamicView}

Look up dynamic view reference from within the collection

Parameters:
Name Type Description
name string

name of dynamic view to retrieve reference of

Source:
Returns:

A reference to the dynamic view with that name

Type
DynamicView

getStage()

(Staging API) create a stage and/or retrieve it

Source:

getTransform(name)

Retrieves a named transform from the collection.

Parameters:
Name Type Description
name string

name of the transform to lookup.

Source:

insert(doc) → {object|array}

Adds object(s) to collection, ensure object(s) have meta properties, clone it if necessary, etc.

Parameters:
Name Type Description
doc object | array

the document (or array of documents) to be inserted

Source:
Returns:

document or documents inserted

Type
object | array
Example
users.insert({
    name: 'Odin',
    age: 50,
    address: 'Asgard'
});

// alternatively, insert array of documents
users.insert([{ name: 'Thor', age: 35}, { name: 'Loki', age: 30}]);

mapReduce(mapFunction, reduceFunction) → {data}

Map Reduce operation

Parameters:
Name Type Description
mapFunction function

function to use as map function

reduceFunction function

function to use as reduce function

Source:
Returns:

The result of your mapReduce operation

Type
data

max()

Source:

maxRecord()

Source:

median(field)

Parameters:
Name Type Description
field string

property name

Source:

min()

Source:

minRecord()

Source:

mode(field)

Parameters:
Name Type Description
field string
Source:

remove(doc)

Remove a document from the collection

Parameters:
Name Type Description
doc object

document to remove from collection

Source:

removeDynamicView(name)

Remove a dynamic view from the collection

Parameters:
Name Type Description
name string

name of dynamic view to remove

Source:

removeTransform(name)

Removes a named collection transform from the collection

Parameters:
Name Type Description
name string

name of collection transform to remove

Source:

removeWhere(query)

Remove all documents matching supplied filter function. For 'mongo-like' querying you should migrate to findAndRemove().

Parameters:
Name Type Description
query function | object

query object to filter on

Source:

setTransform(name, transform)

Updates a named collection transform to the collection

Parameters:
Name Type Description
name string

name to associate with transform

transform object

a transformation object to save into collection

Source:

setTTL(age, interval)

Updates or applies collection TTL settings.

Parameters:
Name Type Description
age int

age (in ms) to expire document from collection

interval int

time (in ms) to clear collection of aged documents.

Source:

stage()

(Staging API) create a copy of an object and insert it into a stage

Source:

stdDev(field)

Calculate standard deviation of a field

Parameters:
Name Type Description
field string
Source:

update(doc)

Updates an object and notifies collection that the document has changed.

Parameters:
Name Type Description
doc object

document to update within the collection

Source:

updateWhere(filterFunction, updateFunction)

Applies a filter function and passes all results to an update function.

Parameters:
Name Type Description
filterFunction function

filter function whose results will execute update

updateFunction function

update function to run against filtered documents

Source:

where(fun) → {array}

Query the collection by supplying a javascript filter function.

Parameters:
Name Type Description
fun function

filter function to run against all collection docs

Source:
Returns:

all documents which pass your filter function

Type
array
Example
var results = coll.where(function(obj) {
  return obj.legs === 8;
});