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> |
(optional) options to configure dynamic view with Properties
|
Returns:
reference to the dynamic view added
- Type
- DynamicView
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 |
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, parameters) → {Resultset}
Chain method, used for beginning a series of chained find() and/or view() operations on a collection.
Parameters:
Name | Type | Description |
---|---|---|
transform |
array | Ordered array of transform step objects similar to chain |
parameters |
object | Object containing properties representing parameters to substitute |
Returns:
(this) resultset, or data array if any map or join functions where called
- Type
- Resultset
clear()
Empties the collection.
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
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) → {Resultset}
Join two collections on specified properties
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
joinData |
array | 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 |
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
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
insertOne(doc, bulkInsert) → {object}
Adds a single object, ensures it has meta properties, clone it if necessary, etc.
Parameters:
Name | Type | Description |
---|---|---|
doc |
object | the document to be inserted |
bulkInsert |
boolean | quiet pre-insert and insert event emits |
Returns:
document or 'undefined' if there was a problem inserting it
- Type
- object
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 |
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;
});