Module: Collection

Collection factory.
Mixes In:
  • DatabaseObject
Source:

Methods

add(expr) → {CollectionAdd}

Create an operation to add one or more documents to the collection.
Parameters:
Name Type Description
expr DocumentOrJSON | Array.<DocumentOrJSON> object with document data
Source:
Throws:
When the input type is invalid.
Type
Error
Returns:
The operation instance.
Type
CollectionAdd
Example
// arguments as single documents
collection.add({ foo: 'baz' }, { bar: 'qux' })

// array of documents
collection.add([{ foo: 'baz' }, { bar: 'qux' }])

addOrReplaceOne(id, data) → {Promise.<Result>}

Create or replace a document with the given id.
Parameters:
Name Type Description
id string document id
data Object document properties
Source:
Returns:
A promise that resolves to the operation result.
Type
Promise.<Result>
Example
collection.addOrReplaceOne('foo', { prop1: 'bar', prop2: 'baz' })

createIndex(name, constraint) → {Promise.<boolean>}

Create a new index.
Parameters:
Name Type Description
name string index name
constraint IndexDefinition index definition
Source:
Returns:
Type
Promise.<boolean>

dropIndex(name) → {Promise.<boolean>}

Drop an Index on a Collection given a name.
Parameters:
Name Type Description
name string Index name
Source:
Returns:
Type
Promise.<boolean>

existsInDatabase() → {Promise.<boolean>}

Check if this collection exists in the database.
Source:
Returns:
Type
Promise.<boolean>

find(expr) → {CollectionFind}

Create an operation to find documents in the collection.
Parameters:
Name Type Description
expr SearchConditionStr filtering criteria
Source:
Returns:
The operation instance.
Type
CollectionFind

getName() → {string}

Retrieve the collection name.
Source:
Returns:
Type
string

getOne(id) → {Object}

Retrieve a single document with the given id.
Parameters:
Name Type Description
id string document id
Source:
Returns:
The document instance.
Type
Object
Example
collection.getOne('1')

getSchema() → {Schema}

Retrieve the schema associated to the collection.
Source:
Returns:
Type
Schema

inspect() → {Object}

Retrieve the collection metadata.
Source:
Returns:
An object containing the relevant metadata.
Type
Object

modify(expr) → {CollectionModify}

Create an operation to modify documents in the collection.
Parameters:
Name Type Description
expr SearchConditionStr filtering criteria
Source:
Returns:
The operation instance.
Type
CollectionModify
Example
// update all documents in a collection
collection.modify('true').set('name', 'bar')

// update documents that match a given condition
collection.modify('name = "foo"').set('name', 'bar')

remove(expr) → {CollectionRemove}

Create an operation to remove documents from the collection.
Parameters:
Name Type Description
expr SearchConditionStr filtering criteria
Source:
Returns:
The operation instance.
Type
CollectionRemove
Example
// remove all documents from a collection
collection.remove('true')

// remove documents that match a given condition
collection.remove('name = "foobar"')

removeOne(id) → {Promise.<Result>}

Remove a single document with the given id.
Parameters:
Name Type Description
id string document id
Source:
Returns:
A promise that resolves to the operation result.
Type
Promise.<Result>
Example
collection.removeOne('1')

replaceOne(id, data) → {Promise.<Result>}

Replace an entire document with a given id.
Parameters:
Name Type Description
id string document id
data Object document properties
Source:
Returns:
A promise that resolves to the operation result.
Type
Promise.<Result>
Example
collection.replaceOne('foo', { prop1: 'bar', prop2: 'baz' })

Type Definitions

FieldDefinition

Index field definition.
Type:
  • Object
Properties:
Name Type Attributes Default Description
field string document path
type string index type (see example)
required boolean <optional>
false allow (or not) `null` values for the column
options number <optional>
describes how to handle GeoJSON documents that contain geometries with coordinate dimensions higher than 2
srid number <optional>
unique value used to unambiguously identify projected, unprojected, and local spatial coordinate system definitions.
Source:
Example
INT [UNSIGNED]
TINYINT [UNSIGNED]
SMALLINT [UNSIGNED]
MEDIUMINT [UNSIGNED]
INTEGER [UNSIGNED]
BIGINT [UNSIGNED]
REAL [UNSIGNED]
FLOAT [UNSIGNED]
DOUBLE [UNSIGNED]
DECIMAL [UNSIGNED]
NUMERIC [UNSIGNED]
DATE
TIME
TIMESTAMP
DATETIME
TEXT[(length)]
GEOJSON (extra options: options, srid)

IndexDefinition

Index overall definition.
Type:
  • Object
Properties:
Name Type Attributes Default Description
type string <optional>
INDEX index type (INDEX or SPATIAL).
fields Array.<FieldDefinition> index field definitions
Source: