Class: MongoRepository<T>
Defined in: service-core/src/database/MongoRepository.ts:36
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:44
Parameters
db
Db
collection
Collection<T>
modelClass?
any
Returns
MongoRepository<T>
Properties
collection
readonlycollection:Collection<T>
Defined in: service-core/src/database/MongoRepository.ts:40
The underlying MongoDB collection that operations are performed against.
db
readonlydb:Db
Defined in: service-core/src/database/MongoRepository.ts:38
The database that the underlying collection belongs to.
modelClass
readonlymodelClass:any
Defined in: service-core/src/database/MongoRepository.ts:42
The model class associated with this repository.
Accessors
collectionName
Get Signature
get collectionName():
string
Defined in: service-core/src/database/MongoRepository.ts:51
The name of the underlying MongoDB collection.
Returns
string
Methods
aggregate()
aggregate(
pipeline,options?):AggregationCursor<any>
Defined in: service-core/src/database/MongoRepository.ts:60
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:68
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:78
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:88
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:98
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:121
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:135
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:144
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:111
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>
save()
save(
doc,options?):Promise<T>
Defined in: service-core/src/database/MongoRepository.ts:165
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()).
Parameters
doc
any
The document to save.
options?
(InsertOneOptions | ReplaceOptions) & { mergeByUid?: boolean | undefined; }
Driver options, plus optionally mergeByUid (see above).
Returns
Promise<T>
The saved document.
updateMany()
updateMany(
filter,update,options?):Promise<UpdateResult<T>>
Defined in: service-core/src/database/MongoRepository.ts:210
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:221
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>>