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
schema object <optional>

schema definition object, validates documents added to collection if defined

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

disableMeta boolean <optional>
false

set to true to disable meta property on documents

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

disableFreeze boolean <optional>
true

when false all docs are frozen

cloneMethod string <optional>
'parse-stringify'

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

ttl int <optional>

age of document (in ms.) before document is considered aged/stale.

ttlInterval int <optional>

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(transform, 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 string | array

named transform or array of transform steps

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

checkAllIndexes(optionsopt) → {Array.<string>}

Perform checks to determine validity/consistency of all binary indices

Parameters:
Name Type Attributes Description
options object <optional>

optional configuration object

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

whether (faster) random sampling should be used

randomSamplingFactor number <optional>
0.10

percentage of total rows to randomly sample

repair boolean <optional>
false

whether to fix problems if they are encountered

Source:
Returns:

array of index names where problems were found.

Type
Array.<string>
Example
// check all indices on a collection, returns array of invalid index names
var result = coll.checkAllIndexes({ repair: true, randomSampling: true, randomSamplingFactor: 0.15 });
if (result.length > 0) {
  results.forEach(function(name) {
    console.log('problem encountered with index : ' + name);
  });
}

checkIndex(property, optionsopt) → {boolean}

Perform checks to determine validity/consistency of a binary index

Parameters:
Name Type Attributes Description
property string

name of the binary-indexed property to check

options object <optional>

optional configuration object

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

whether (faster) random sampling should be used

randomSamplingFactor number <optional>
0.10

percentage of total rows to randomly sample

repair boolean <optional>
false

whether to fix problems if they are encountered

Source:
Returns:

whether the index was found to be valid (before optional correcting).

Type
boolean
Example
// full test
var valid = coll.checkIndex('name');
// full test with repair (if issues found)
valid = coll.checkIndex('name', { repair: true });
// random sampling (default is 10% of total document count)
valid = coll.checkIndex('name', { randomSampling: true });
// random sampling (sample 20% of total document count)
valid = coll.checkIndex('name', { randomSampling: true, randomSamplingFactor: 0.20 });
// random sampling (implied boolean)
valid = coll.checkIndex('name', { randomSamplingFactor: 0.20 });
// random sampling with repair (if issues found)
valid = coll.checkIndex('name', { repair: true, randomSampling: true });

clear(optionsopt)

Empties the collection.

Parameters:
Name Type Attributes Description
options object <optional>

configure clear behavior

Properties
Name Type Attributes Default Description
removeIndices bool <optional>
false

whether to remove indices in addition to data

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

ensureAllIndexes(force)

Ensure all binary indices

Parameters:
Name Type Description
force boolean

whether to force rebuild of existing lazy binary indices

Source:

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

exists(query) → {boolean}

Checks if a document exists in the collection.

Parameters:
Name Type Description
query object

query object used to perform search with

Source:
Returns:

True if document exists, false if not.

Type
boolean

extract()

Source:

extractNumerical()

Source:

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

$ctrl 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, overrideAdaptiveIndicesopt) → {object|array}

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

Parameters:
Name Type Attributes Description
doc object | array

the document (or array of documents) to be inserted

overrideAdaptiveIndices boolean <optional>

(optional) if true, adaptive indicies will be temporarily disabled and then fully rebuilt after batch. This will be faster for large inserts, but slower for small/medium inserts in large collections

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: 'ControlDB', 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:

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:

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:

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;
});