Skip to main content

Class: MongoRepository<T>

Defined in: service-core/src/database/MongoRepository.ts:28

Provides a lightweight repository for performing common operations against a single MongoDB collection using the native mongodb driver. Instances of this class are obtained via MongoConnection.getRepository.

Note that this class only references the mongodb package via type-only imports and is therefore safe to load when the optional mongodb peer dependency is not installed.

Type Parameters

T

T extends Document = any

Constructors

Constructor

new MongoRepository<T>(db, collection, modelClass?): MongoRepository<T>

Defined in: service-core/src/database/MongoRepository.ts:36

Parameters

db

Db

collection

Collection<T>

modelClass?

any

Returns

MongoRepository<T>

Properties

collection

readonly collection: Collection<T>

Defined in: service-core/src/database/MongoRepository.ts:32

The underlying MongoDB collection that operations are performed against.


db

readonly db: Db

Defined in: service-core/src/database/MongoRepository.ts:30

The database that the underlying collection belongs to.


modelClass

readonly modelClass: any

Defined in: service-core/src/database/MongoRepository.ts:34

The model class associated with this repository.

Accessors

collectionName

Get Signature

get collectionName(): string

Defined in: service-core/src/database/MongoRepository.ts:43

The name of the underlying MongoDB collection.

Returns

string

Methods

aggregate()

aggregate(pipeline): AggregationCursor<any>

Defined in: service-core/src/database/MongoRepository.ts:52

Executes the given aggregation pipeline against the collection and returns the resulting cursor.

Parameters

pipeline

Document[]

The aggregation pipeline stages to execute.

Returns

AggregationCursor<any>


clear()

clear(): Promise<void>

Defined in: service-core/src/database/MongoRepository.ts:60

Drops the entire collection from the database, removing all documents and indexes. Does nothing if the collection does not exist.

Returns

Promise<void>


count()

count(filter?, options?): Promise<number>

Defined in: service-core/src/database/MongoRepository.ts:70

Returns the number of documents matching the given filter.

Parameters

filter?

Filter<T>

The filter for the count

options?

CountDocumentsOptions

Optional settings for the command

Returns

Promise<number>


deleteMany()

deleteMany(filter, options?): Promise<DeleteResult>

Defined in: service-core/src/database/MongoRepository.ts:80

Deletes all documents matching the given filter.

Parameters

filter

Filter<T>

The query filter to match documents against.

options?

DeleteOptions

Optional settings for the command

Returns

Promise<DeleteResult>


deleteOne()

deleteOne(filter?, options?): Promise<DeleteResult>

Defined in: service-core/src/database/MongoRepository.ts:90

Deletes the first document matching the given filter.

Parameters

filter?

Filter<T>

The query filter to match documents against.

options?

DeleteOptions

Optional settings for the command

Returns

Promise<DeleteResult>


distinct()

distinct(field, filter?): Promise<any[]>

Defined in: service-core/src/database/MongoRepository.ts:100

Returns the list of distinct values of the given field for all documents matching the given filter.

Parameters

field

string

The name of the document field to return distinct values of.

filter?

Filter<T>

The query filter to match documents against.

Returns

Promise<any[]>


find()

find(filter?, options?): FindCursor<T>

Defined in: service-core/src/database/MongoRepository.ts:110

Returns all documents matching the given filter.

Parameters

filter?

Filter<T>

The query filter to match documents against.

options?

FindOptions

Optional settings for the command

Returns

FindCursor<T>


findOne()

findOne(filter): Promise<T | null>

Defined in: service-core/src/database/MongoRepository.ts:119

Returns the first document matching the given filter.

Parameters

filter

any

The query filter to match documents against.

Returns

Promise<T | null>


save()

save(doc): Promise<T>

Defined in: service-core/src/database/MongoRepository.ts:131

Saves the given document to the collection. If the document has an existing _id the stored document is replaced (inserting if missing), otherwise the document is inserted and its newly assigned _id is set on the returned object.

Parameters

doc

any

The document to save.

Returns

Promise<T>

The saved document.


updateMany()

updateMany(filter, update, options?): Promise<UpdateResult<T>>

Defined in: service-core/src/database/MongoRepository.ts:157

Updates all documents matching the given filter with the provided update operations.

Parameters

filter

any

The query filter to match documents against.

update

any

The update operations (e.g. $set) to apply.

options?

UpdateOptions

Optional settings for the command

Returns

Promise<UpdateResult<T>>


updateOne()

updateOne(filter, update, options?): Promise<UpdateResult<T>>

Defined in: service-core/src/database/MongoRepository.ts:168

Updates the first document matching the given filter with the provided update operations.

Parameters

filter

any

The query filter to match documents against.

update

any

The update operations (e.g. $set) to apply.

options?

UpdateOptions

Optional settings for the command

Returns

Promise<UpdateResult<T>>