Skip to main content

Class: MongoRepository<T>

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

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:45

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:41

The underlying MongoDB collection that operations are performed against.


db​

readonly db: Db

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

The database that the underlying collection belongs to.


modelClass​

readonly modelClass: any

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

The model class associated with this repository.

Accessors​

collectionName​

Get Signature​

get collectionName(): string

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

The name of the underlying MongoDB collection.

Returns​

string

Methods​

aggregate()​

aggregate(pipeline, options?): AggregationCursor<any>

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

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

Parameters​

pipeline​

Document[]

The aggregation pipeline stages to execute.

options?​

AggregateOptions & Abortable

Returns​

AggregationCursor<any>


clear()​

clear(options?): Promise<void>

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

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

Parameters​

options?​

DeleteOptions & Abortable

Returns​

Promise<void>


count()​

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

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

Returns the number of documents matching the given filter.

Parameters​

filter?​

Filter<T>

The filter for the count

options?​

CountDocumentsOptions & Abortable

Optional settings for the command

Returns​

Promise<number>


deleteMany()​

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

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

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:99

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?, options?): Promise<any[]>

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

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.

options?​

DistinctOptions

Returns​

Promise<any[]>


find()​

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

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

Returns all documents matching the given filter.

Parameters​

filter?​

Filter<T>

The query filter to match documents against.

options?​

FindOptions & Abortable

Optional settings for the command

Returns​

FindCursor<T>


findOne()​

findOne(filter, options?): Promise<T | null>

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

Returns the first document matching the given filter.

Parameters​

filter​

any

The query filter to match documents against.

options?​

FindOptions & Abortable

Returns​

Promise<T | null>


findOneAndDelete()​

findOneAndDelete(filter, options?): Promise<T | null>

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

Atomically finds and deletes the first document matching the given filter, returning the document that was deleted (or null if none matched). This is a single round trip rather than a separate findOne() + deleteOne(), so a caller that needs to know exactly what it removed (e.g. to snapshot it for a possible restore) isn't racing its own read against a concurrent write to the same document.

Parameters​

filter​

Filter<T>

The query filter to match documents against.

options?​

FindOneAndDeleteOptions

Optional settings for the command

Returns​

Promise<T | null>


findOneAndUpdate()​

findOneAndUpdate(filter, update, options?): Promise<T | null>

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

Atomically finds the first document matching the given filter, applies the given update operations to it and returns the resulting document (or null if none matched). By default the document is returned as it was after the update; pass returnDocument: "before" to get the original. A single round trip, so a caller that needs the exact document its own update produced isn't racing a separate follow-up read against a concurrent write to the same document (see RepoUtils.update()).

Parameters​

filter​

any

The query filter to match documents against.

update​

any

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

options?​

FindOneAndUpdateOptions

Optional settings for the command

Returns​

Promise<T | null>


save()​

save(doc, options?): Promise<T>

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

Saves the given document to the collection. By default, 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. This is the behavior a trackChanges-style caller relies on to keep multiple documents per uid (one per version): a document built for a fresh version deliberately carries no _id yet, and must always become a genuinely new row, never merged into an existing one.

Pass mergeByUid: true to instead match (and replace, or insert if missing) by the document's uid, the framework's true logical primary key, regardless of whether _id was carried forward from a prior read. Useful for a single-row-per-uid document that may have been built fresh (e.g. by spreading an update onto a plain object) without preserving _id, where the default _id-only behavior would insert a second, duplicate document instead of updating the existing one (see ACLUtils.saveACL()).

Pass insertOnly: true to always insert, even when doc carries an _id: the document is inserted with that _id, and the driver's duplicate-key error (code 11000) is thrown if a document with it already exists. Use it for any create-style write whose _id may have come from untrusted input, where the default _id-present behavior would silently replace whichever existing document owns that _id (see RepoUtils.create()). Takes precedence over mergeByUid.

Parameters​

doc​

any

The document to save.

options?​

(InsertOneOptions | ReplaceOptions) & { mergeByUid?: boolean | undefined; insertOnly?: boolean | undefined; }

Driver options, plus optionally mergeByUid or insertOnly (see above).

Returns​

Promise<T>

The saved document.


updateMany()​

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

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

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:253

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>>