Collection factory.
- Mixes In:
Methods
-
add(expr)
-
Create an operation to add one or more documents to the collection.
Parameters:
Name Type Description expr
DocumentOrJSON | Array.<DocumentOrJSON> object with document data Throws:
-
When the input type is invalid.
- Type
- Error
Returns:
The operation instance.- Type
- module:CollectionAdd
Example
// arguments as single documents collection.add({ foo: 'baz' }, { bar: 'qux' }) // array of documents collection.add([{ foo: 'baz' }, { bar: 'qux' }])
-
-
addOrReplaceOne(id, data)
-
Create or replace a document with the given id.
Parameters:
Name Type Description id
string document id data
Object document properties Returns:
A promise that resolves to the operation result.- Type
- Promise.<module:Result>
Example
collection.addOrReplaceOne('foo', { prop1: 'bar', prop2: 'baz' })
-
count()
-
Retrieve the total number of documents in the collection.
Returns:
- Type
- Promise.<number>
-
createIndex(name, constraint)
-
Create a new index.
Parameters:
Name Type Description name
string index name constraint
IndexDefinition index definition Returns:
- Type
- Promise.<boolean>
-
dropIndex(name)
-
Drop an Index on a Collection given a name.
Parameters:
Name Type Description name
string Index name Returns:
- Type
- Promise.<boolean>
-
existsInDatabase()
-
Check if this collection exists in the database.
Returns:
- Type
- Promise.<boolean>
-
find(expr)
-
Create an operation to find documents in the collection.
Parameters:
Name Type Description expr
SearchConditionStr filtering criteria Returns:
The operation instance. -
getName()
-
Retrieve the collection name.
Returns:
- Type
- string
-
getOne(id)
-
Retrieve a single document with the given id.
Parameters:
Name Type Description id
string document id Returns:
The document instance.- Type
- Object
Example
collection.getOne('1')
-
getSchema()
-
Retrieve the schema associated to the collection.
Returns:
- Type
- module:Schema
-
inspect()
-
Retrieve the collection metadata.
Returns:
An object containing the relevant metadata.- Type
- Object
-
modify(expr)
-
Create an operation to modify documents in the collection.
Parameters:
Name Type Description expr
SearchConditionStr filtering criteria Returns:
The operation instance.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)
-
Create an operation to remove documents from the collection.
Parameters:
Name Type Description expr
SearchConditionStr filtering criteria Returns:
The operation instance.Example
// remove all documents from a collection collection.remove('true') // remove documents that match a given condition collection.remove('name = "foobar"')
-
removeOne(id)
-
Remove a single document with the given id.
Parameters:
Name Type Description id
string document id Returns:
A promise that resolves to the operation result.- Type
- Promise.<module:Result>
Example
collection.removeOne('1')
-
replaceOne(id, data)
-
Replace an entire document with a given id.
Parameters:
Name Type Description id
string document id data
Object document properties Returns:
A promise that resolves to the operation result.- Type
- Promise.<module:Result>
Example
collection.replaceOne('foo', { prop1: 'bar', prop2: 'baz' })
Type Definitions
-
FieldDefinition
-
Index field definition.
Type:
- Object
Properties:
Name Type Argument 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. 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 Argument Default Description type
string <optional>
INDEX index type (INDEX or SPATIAL). fields
Array.<FieldDefinition> index field definitions