new Collection( name )

Description

Creates a Collection instance.

Parameters
Name Type Description
name string

Name of the collection.

Details

EventEmitter

Members


documents

Description

Get all documents of the collection.

Returns

All documents of the collection.

Details

name

Description

Get the name of the collection.

Returns

Name of the collection.

Details

schema

Description

Get the registered schema of the collection.

Returns

Registered schema of the collection.

Details

hasSchema

Description

Get whether the collection has a schema registered.

Returns

Whether the collection has a schema registered.

Details

created

Description

Get the date and time of creation for this collection.

Returns

Date and time of creation.

Details

firstDocument

Description

Get the first document of the collection.

Returns

First document of the collection.

Details

lastDocument

Description

Get the last document of the collection.

Returns

Last document of the collection.

Details

size

Description

Get the amount of documents of the collection.

Returns

Amount of documents of the collection.

Details

Methods


insert( ...docs ) → {Array.<string>}

Description

Insert new documents into the collection.

Parameters
Name Type Attributes Description
docs object <repeatable>

Documents to insert. Passing an array of documents is also supported.

Returns

IDs of the inserted documents.

Fires
Details

insertOne( doc [, emitEvent ] ) → {string}

Description

Insert a single document into the collection.

Parameters
Name Type Attributes Default Description
doc object

Document to insert.

emitEvent boolean <optional>
true

Whether to emit the insert event.

Returns

ID of the inserted document.

Fires
Details

find( filter, resultInstance ) → {Array.<object>|Result}

Description

Find all documents in the collection that match the query or pass the filter function.

Parameters
Name Type Default Description
filter object | function

Query object or filter function which will be applied on each document.

resultInstance boolean false

Whether to return a Result instance with the matching documents.

Returns

Matching documents.

Examples
// Find all documents where the value of the property price is greater than or equal to 50.
			collection.find({ price: { greaterThanOrEqual: 50 } });
// Same result but using a function.
			collection.find((doc) => doc.price >= 50);
Fires
Details

where( filter, resultInstance ) → {Array.<object>|Result}

Description

Find all documents in the collection that match the query or pass the filter function.

Parameters
Name Type Default Description
filter object | function

Query object or filter function which will be applied on each document.

resultInstance boolean false

Whether to return a Result instance with the matching documents.

Returns

Matching documents.

Examples
// Find all documents where the value of the property price is greater than or equal to 50.
			collection.find({ price: { greaterThanOrEqual: 50 } });
// Same result but using a function.
			collection.find((doc) => doc.price >= 50);
Fires
Details

where( filter, resultInstance ) → {Array.<object>|Result}

Description

Find all documents in the collection that match the query or pass the filter function.

Parameters
Name Type Default Description
filter object | function

Query object or filter function which will be applied on each document.

resultInstance boolean false

Whether to return a Result instance with the matching documents.

Returns

Matching documents.

Examples
// Find all documents where the value of the property price is greater than or equal to 50.
			collection.find({ price: { greaterThanOrEqual: 50 } });
// Same result but using a function.
			collection.find((doc) => doc.price >= 50);
Fires
Details

findOne( filter ) → {object}

Description

Find the first document in the collection that matches the query or passes the filter function.

Parameters
Name Type Description
filter object | function

Query object or filter function which will be applied on each document.

Returns

Matching document.

Examples
// Find the first document where the value of the roperty price is greater than or equal to 50.
			collection.findOne({ price: { greaterThanOrEqual: 50 } });
// Same result but using a function.
			collection.findOne((doc) => doc.price >= 50);
Fires
Details

findById( id ) → {object}

Description

Find the document with the matching ID.

Parameters
Name Type Description
id string

ID of the document to retrieve.

Returns

Matching document.

Examples
collection.findById('1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed');
Details

removeExact( doc [, first ] ) → {object|Array.<object>}

Description

Remove all documents matching the given document from the collection.

Parameters
Name Type Attributes Default Description
doc object

Document(-s) to remove.

first boolean <optional>
false

Whether to remove only the first matching document.

Returns

Removed document(-s).

Examples
// Remove the first matching document.
			collection.removeExact({ price: 50 }, true);
// Remove all matching documents.
			collection.removeExact({ price: 50 });
Fires
Details

remove( filter ) → {Array.<object>}

Description

Remove all documents in the collection that match the query or passes the filter function.

Parameters
Name Type Description
filter object | function

Query object or filter function which will be applied on each document.

Returns

Removed documents.

Examples
// Remove all documents where the value of the
			// property price is greater than or equal to 50.
			collection.remove({ price: { greaterThanOrEqual: 50 } });
// Same result but using a function.
			collection.remove((doc) => doc.price >= 50);
Fires
Details

removeOne( filter ) → {Array.<object>}

Description

Remove the first document in the collection that matches the query or passes the filter function.

Parameters
Name Type Description
filter object | function

Query object or filter function which will be applied on each document.

Returns

Removed document.

Examples
// Remove the first document where the value of the
			// property price is greater than or equal to 50.
			collection.removeOne({ price: { greaterThanOrEqual: 50 } });
			// or
			collection.removeOne((doc) => doc.price >= 50);
Fires
Details

removeById( id ) → {object}

Description

Remove the document with the matching ID.

Parameters
Name Type Description
id string

ID of the document to remove.

Returns

Removed document.

Examples
collection.removeById('1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed');
Fires
Details

update( filter, updater ) → {Array.<object>}

Description

Update all documents in the collection that match the query or pass the custom filter function.

Parameters
Name Type Description
filter object | function

Query object or filter function which will be applied on each document.

updater object | function

Object or update function which will be applied on each filtered document.

Returns

Updated documents.

Examples
// Update all documents where the value of the property price is greater than or equal to 50 and set it to 49.99.
			collection.update(
			  (doc) => doc.price >= 50,
			  { price: 49.99 }
			);
Fires
Details

updateById( id, updater ) → {object}

Description

Update the document with the matching ID.

Parameters
Name Type Description
id string

ID of the document to update.

updater object | function

Object or update function which will be applied on each filtered document.

Returns

Updated document.

Examples
collection.updateById(
			 '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
			 { price: 49.99 }
			);
Fires
Details

registerSchema( schema ) → {Schema}

Description

Register a schema to validate documents inserted into the collection.

Parameters
Name Type Description
schema Schema | object

Schema to validate documents with.

Returns

Registered schema.

Details

unregisterSchema() → {Schema}

Description

Unregister the schema which ultimately stops the collection from validating any further documents.

Returns

Unregistered schema if there was a registered schema beforehand.

Details

cleanWithSchema( [ schema ] ) → {Array.<object>}

Description

Clean the collection from invalid documents using the passed in or registered schema.

Parameters
Name Type Attributes Description
schema Schema <optional>

Schema instance to clean collection with.

Returns

Removed documents.

Details

clear() → {Array.<object>}

Description

Clear the collection by deleting all of its documents.

Returns

All of the removed documents.

Details