Constructor
new MongoDatabase(configuration)
Defines a MongoDB Database.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
configuration |
Object | A database configuration object Properties
|
Extends
Members
client :Object
The MongoDB client instance.
Type:
- Object
(readonly) configuration :Object
The storage configuration.
Type:
- Object
- Inherited From:
- Source:
(readonly) db :Object
The connected database.
Type:
- Object
(readonly) host :String
Database host.
Type:
- String
- Inherited From:
- Source:
(readonly) name :String
Database name.
Type:
- String
- Inherited From:
- Source:
(readonly) password :String
Database user password.
Type:
- String
- Inherited From:
- Source:
(readonly) port :Number
Database port.
Type:
- Number
- Inherited From:
- Source:
(readonly) replicaSet :String
The name of the replica set.
Type:
- String
(readonly) seedlist :String
A comma separated list of secondary servers.
Type:
- String
(readonly) username :String
Database user name.
Type:
- String
- Inherited From:
- Source:
Methods
(static) buildFields(fields, doesInclude) → {Object}
Builds MongoDb fields projection.
Parameters:
Name | Type | Description |
---|---|---|
fields |
Array | The list of fields to include or exclude |
doesInclude |
Boolean | true to include fields and exclude all other fields or false to exclude fields and include all other fields |
Returns:
The MongoDB projection description object
- Type
- Object
(static) buildFilter(resourceFilter) → {Object}
Builds MongoDb filter from a ResourceFilter.
Parameters:
Name | Type | Description |
---|---|---|
resourceFilter |
module:storages/ResourceFilter~ResourceFilter | The common resource filter |
Throws:
-
If an operation is not supported
- Type
- TypeError
Returns:
The MongoDB like filter description object
- Type
- Object
(static) buildSort(sortopt) → {Object}
Builds MongoDB sort object.
Concretely it just replaces "score" by "{ $meta: 'textScore' }", "asc" by 1 and "desc" by -1.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
sort |
Object |
<optional> |
The list of fields to sort by with the field name as key and the sort order as value (e.g. {field1: 'asc', field2: 'desc', field3: 'score'}) |
Returns:
The MongoDB sort description object
- Type
- Object
add(collection, documents, callback)
Inserts several documents into a collection.
Parameters:
Name | Type | Description |
---|---|---|
collection |
String | The collection to work on |
documents |
Array | Document(s) to insert into the collection |
callback |
module:storages/MongoDatabase~MongoDatabase~addCallback | The function to call when it's done |
- Overrides:
- Source:
close(callback)
Closes connection to the database.
Parameters:
Name | Type | Description |
---|---|---|
callback |
callback | The function to call when connection is closed |
- Overrides:
- Source:
connect(callback)
Establishes connection to the database.
Parameters:
Name | Type | Description |
---|---|---|
callback |
callback | The function to call when connection to the database is established |
- Overrides:
- Source:
createIndexes(collection, indexes, callback)
Creates indexes for a collection.
Parameters:
Name | Type | Description |
---|---|---|
collection |
String | The collection to work on |
indexes |
Array | A list of indexes using MongoDB format |
callback |
module:storages/MongoDatabase~MongoDatabase~createIndexesCallback | The function to call when it's done |
- Overrides:
- Source:
dropIndex(collection, indexName, callback)
Drops an index from a collection.
Parameters:
Name | Type | Description |
---|---|---|
collection |
String | The collection to work on |
indexName |
Array | The name of the index to drop |
callback |
module:storages/MongoDatabase~MongoDatabase~dropIndexCallback | The function to call when it's done |
- Overrides:
- Source:
get(collection, filteropt, fieldsopt, limitopt, pageopt, sort, callback)
Fetches documents from the collection.
Parameters:
Name | Type | Attributes | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
collection |
String | The collection to work on |
|||||||||||||
filter |
module:storages/ResourceFilter~ResourceFilter |
<optional> |
Rules to filter documents |
||||||||||||
fields |
Object |
<optional> |
Expected resource fields to be included or excluded from the response, by default all fields are returned. Only "exclude" or "include" can be specified, not both Properties
|
||||||||||||
limit |
Number |
<optional> |
A limit number of documents to retrieve (10 by default) |
||||||||||||
page |
Number |
<optional> |
The page number started at 0 for the first page |
||||||||||||
sort |
Object | The list of fields to sort by with the field name as key and the sort order as value (e.g. {field1: 'asc', field2: 'desc', field3: 'score'}) |
|||||||||||||
callback |
module:storages/MongoDatabase~MongoDatabase~getCallback | The function to call when it's done |
- Overrides:
- Source:
getIndexes(collection, callback)
Gets the list of indexes for a collection.
Parameters:
Name | Type | Description |
---|---|---|
collection |
String | The collection to work on |
callback |
module:storages/MongoDatabase~MongoDatabase~getIndexesCallback | The function to call when it's done |
- Overrides:
- Source:
getOne(collection, filteropt, fieldsopt, callback)
Fetches a single document from the storage.
Parameters:
Name | Type | Attributes | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
collection |
String | The collection to work on |
|||||||||||||
filter |
module:storages/ResourceFilter~ResourceFilter |
<optional> |
Rules to filter documents |
||||||||||||
fields |
Object |
<optional> |
Expected document fields to be included or excluded from the response, by default all fields are returned. Only "exclude" or "include" can be specified, not both Properties
|
||||||||||||
callback |
module:storages/MongoDatabase~MongoDatabase~getOneCallback | The function to call when it's done |
- Overrides:
- Source:
getStore(collection) → {Object}
Gets an express-session store for this database.
Parameters:
Name | Type | Description |
---|---|---|
collection |
String | The collection to work on |
- Overrides:
- Source:
Returns:
An express-session store
- Type
- Object
remove(collection, filteropt, callback)
Removes several documents from a collection.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
collection |
String | The collection to work on |
|
filter |
module:storages/ResourceFilter~ResourceFilter |
<optional> |
Rules to filter documents to remove |
callback |
module:storages/MongoDatabase~MongoDatabase~removeCallback | The function to call when it's done |
- Overrides:
- Source:
removeCollection(collection, callback)
Removes a collection from the database.
Parameters:
Name | Type | Description |
---|---|---|
collection |
String | The collection to work on |
callback |
callback | The function to call when it's done |
- Overrides:
- Source:
removeField(collection, property, filteropt, callback)
Removes a property from documents of a collection.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
collection |
String | The collection to work on |
|
property |
String | The name of the property to remove |
|
filter |
module:storages/ResourceFilter~ResourceFilter |
<optional> |
Rules to filter documents to update |
callback |
module:storages/MongoDatabase~MongoDatabase~removeFieldCallback | The function to call when it's done |
- Overrides:
- Source:
renameCollection(collection, target, callback)
Renames a collection.
Parameters:
Name | Type | Description |
---|---|---|
collection |
String | The collection to work on |
target |
String | The new name of the collection |
callback |
callback | The function to call when it's done |
- Overrides:
- Source:
updateOne(collection, filteropt, data, callback)
Updates a document from collection.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
collection |
String | The collection to work on |
|
filter |
module:storages/ResourceFilter~ResourceFilter |
<optional> |
Rules to filter the document to update |
data |
Object | The modifications to perform |
|
callback |
module:storages/MongoDatabase~MongoDatabase~updateOneCallback | The function to call when it's done |
- Overrides:
- Source:
Type Definitions
addCallback(error, total, documents)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
total |
Number | Undefined | The total amount of documents inserted |
documents |
Array | Undefined | The list of inserted documents |
createIndexesCallback(error, result)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
result |
Object | Undefined | Information about the operation |
dropIndexCallback(error, result)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
result |
Object | Undefined | Information about the operation |
getCallback(error, documents, pagination, limit, page, pages, size)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
documents |
Array | Undefined | The list of retrieved documents |
pagination |
Object | Undefined | Pagination information |
limit |
Number | Undefined | The specified limit |
page |
Number | Undefined | The actual page |
pages |
Number | Undefined | The total number of pages |
size |
Number | Undefined | The total number of documents |
getIndexesCallback(error, indexes)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
indexes |
Array | Undefined | The list of indexes |
getOneCallback(error, document)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
document |
Object | Undefined | The document |
removeCallback(error, total)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
total |
Number | Undefined | The number of deleted documents |
removeFieldCallback(error, total)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
total |
Number | Undefined | The number of updated documents |
updateOneCallback(error, total)
Parameters:
Name | Type | Description |
---|---|---|
error |
Error | null | The error if an error occurred, null otherwise |
total |
Number | Undefined | 1 if everything went fine |