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
|
- Implements:
- Source:
- See:
-
- Loki#addCollection for normal creation of collections
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
|
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 |
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 |
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 |
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 |
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
|
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
|
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
|
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 |
configureOptions()
Will allow reconfiguring certain collection options.
Parameters:
Name | Type | Description |
---|---|---|
options.adaptiveBinaryIndices |
boolean | collection indices will be actively rebuilt rather than lazily |
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 |
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 |
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 |
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
|
Returns:
Result of the mapping operation
- Type
- Resultset
extract()
extractNumerical()
find(query) → {array}
Parameters:
Name | Type | Description |
---|---|---|
query |
object | 'mongo-like' query object |
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 |
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 |
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 |
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] |
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 |
Returns:
A reference to the dynamic view with that name
- Type
- DynamicView
getStage()
(Staging API) create a stage and/or retrieve it
getTransform(name)
Retrieves a named transform from the collection.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | name of the transform to lookup. |
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 |
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 |
Returns:
The result of your mapReduce operation
- Type
- data
max()
maxRecord()
median(field)
Parameters:
Name | Type | Description |
---|---|---|
field |
string | property name |
min()
minRecord()
mode(field)
Parameters:
Name | Type | Description |
---|---|---|
field |
string |
remove(doc)
Remove a document from the collection
Parameters:
Name | Type | Description |
---|---|---|
doc |
object | document to remove from collection |
removeDynamicView(name)
Remove a dynamic view from the collection
Parameters:
Name | Type | Description |
---|---|---|
name |
string | name of dynamic view to remove |
removeTransform(name)
Removes a named collection transform from the collection
Parameters:
Name | Type | Description |
---|---|---|
name |
string | name of collection transform to remove |
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 |
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 |
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. |
stage()
(Staging API) create a copy of an object and insert it into a stage
stdDev(field)
Calculate standard deviation of a field
Parameters:
Name | Type | Description |
---|---|---|
field |
string |
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 |
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 |
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 |
Returns:
all documents which pass your filter function
- Type
- array
Example
var results = coll.where(function(obj) {
return obj.legs === 8;
});