Skip to main content

MongoCollectionService

import { MongoCollectionService } from '@opra/mongodb';

Hierarchy: ServiceBaseMongoServiceMongoEntityServiceMongoCollectionService


Constructor

new MongoCollectionService<T>(dataType: Type | string, options?: MongoCollectionService.Options)

Properties

PropertyTypeDescription
defaultLimitnumberMaximum records returned by findMany when no limit option is given

Methods

assert

assert(id: AnyId, options?: ExistsOptions<T>): Promise<void>

Throws ResourceNotAvailableError if no document with the given _id exists.

optionsExistsOptions<T>


create

create(input: PartialDTO<T>, options: RequiredSome<CreateOptions, 'projection'>): Promise<PartialDTO<T>>
create(input: PartialDTO<T>, options?: CreateOptions): Promise<T>

Inserts a new document and fetches it back. If projection is supplied the return type narrows to PartialDTO<T>.

optionsCreateOptions


count

count(options?: CountOptions<T>): Promise<number>

Returns the number of documents matching the filter.

optionsCountOptions<T>


delete

delete(id: AnyId, options?: DeleteOptions<T>): Promise<number>

Removes the document with the given _id. Returns the number of deleted documents.

optionsDeleteOptions<T>


deleteMany

deleteMany(options?: DeleteManyOptions<T>): Promise<number>

Removes all documents matching the filter. Returns the number of deleted documents.

optionsDeleteManyOptions<T>


distinct

distinct(field: string, options?: DistinctOptions<T>): Promise<any[]>

Returns distinct values for a field across documents matching the filter.

optionsDistinctOptions<T>


exists

exists(id: AnyId, options?: ExistsOptions<T>): Promise<boolean>

Returns true if a document with the given _id exists.

optionsExistsOptions<T>


existsOne

existsOne(options?: ExistsOptions<T>): Promise<boolean>

Returns true if at least one document matches the filter.

optionsExistsOptions<T>


findById

findById(id: AnyId, options: RequiredSome<FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>
findById(id: AnyId, options?: FindOneOptions<T>): Promise<T | undefined>

Fetches a document by _id. Returns undefined if not found.

optionsFindOneOptions<T>


findOne

findOne(options: RequiredSome<FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>
findOne(options?: FindOneOptions<T>): Promise<T | undefined>

Fetches the first document matching the filter. Returns undefined if not found.

optionsFindOneOptions<T>


findMany

findMany(options: RequiredSome<FindManyOptions<T>, 'projection'>): Promise<PartialDTO<T>[]>
findMany(options?: FindManyOptions<T>): Promise<T[]>

Fetches a list of documents. Applies defaultLimit when options.limit is not specified.

optionsFindManyOptions<T>


findManyWithCount

findManyWithCount(options: RequiredSome<FindManyOptions<T>, 'projection'>): Promise<{ count: number; items: PartialDTO<T>[] }>
findManyWithCount(options?: FindManyOptions<T>): Promise<{ count: number; items: T[] }>

Same as findMany but also returns the total count of matching documents before pagination.

optionsFindManyOptions<T>


get

get(id: AnyId, options: RequiredSome<FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T>>
get(id: AnyId, options?: FindOneOptions<T>): Promise<T>

Fetches a document by _id. Throws ResourceNotAvailableError if not found.

optionsFindOneOptions<T>


replace

replace(id: AnyId, input: PartialDTO<T>, options: RequiredSome<ReplaceOptions<T>, 'projection'>): Promise<PartialDTO<T>>
replace(id: AnyId, input: PartialDTO<T>, options?: CreateOptions): Promise<T>

Replaces the full document with the given _id and returns the new document.

optionsReplaceOptions<T>


update

update(id: AnyId, input: MongoPatchDTO<T> | UpdateFilter<T>, options: RequiredSome<UpdateOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>
update(id: AnyId, input: MongoPatchDTO<T> | UpdateFilter<T>, options?: UpdateOneOptions<T>): Promise<T | undefined>

Applies a partial update to the document with the given _id and returns the updated document. Returns undefined if not found.

Accepts either a MongoPatchDTO<T> (OPRA patch object) or a raw MongoDB UpdateFilter<T>. The two forms are mutually exclusive.

optionsUpdateOneOptions<T>


updateOnly

updateOnly(id: AnyId, input: MongoPatchDTO<T> | UpdateFilter<T>, options?: UpdateOneOptions<T>): Promise<number>

Same as update but does not fetch and return the updated document. Returns the number of matched documents.

optionsUpdateOneOptions<T>


updateMany

updateMany(input: MongoPatchDTO<T> | UpdateFilter<T>, options?: UpdateManyOptions<T>): Promise<number>

Applies a partial update to all documents matching the filter. Returns the number of matched documents.

optionsUpdateManyOptions<T>


Interfaces

MongoCollectionService.Options

Extends MongoEntityService.OptionsMongoService.Options.

OptionTypeDefaultDescription
defaultLimitnumber10Default maximum number of records returned by findMany

CreateOptions

Extends mongodb.InsertOneOptions.

OptionTypeDescription
projectionstring | string[] | Document | '*'Fields to return in the fetched-back document. Omit for all fields.

CountOptions

Extends mongodb.CountOptions.

OptionTypeDescription
filterFilterInput<T>Filter criteria to count.

DeleteOptions

Extends mongodb.DeleteOptions.

OptionTypeDescription
filterFilterInput<T>Additional filter criteria for the delete.

DeleteManyOptions

Extends mongodb.DeleteOptions.

OptionTypeDescription
filterFilterInput<T>Filter criteria — only matching documents are deleted.

DistinctOptions

Extends mongodb.DistinctOptions.

OptionTypeDescription
filterFilterInput<T>Filter criteria to apply before collecting distinct values.

ExistsOptions

Extends mongodb.CommandOperationOptions (minus session).

OptionTypeDescription
filterFilterInput<T>Additional filter criteria for the existence check.

FindOneOptions

Same as FindManyOptions<T> without the limit property.


FindManyOptions

Extends mongodb.AggregateOptions.

OptionTypeDescription
filterFilterInput<T>Filter criteria (maps to a $match aggregation stage).
projectionstring | string[] | Document | '*'Fields to include. '*' returns all fields.
sortstring[]Sort directives, e.g. ['name', '-createdAt'] (prefix - for descending).
limitnumberMaximum number of documents to return. Defaults to defaultLimit.
skipnumberNumber of documents to skip before returning results.
preStagesmongodb.Document[]Aggregation stages inserted before the filter stage.
postStagesmongodb.Document[]Aggregation stages appended after the limit stage.
noDecodebooleanSkip OPRA output decoding and return raw MongoDB documents.

ReplaceOptions

Extends mongodb.FindOneAndReplaceOptions (minus projection).

OptionTypeDescription
projectionstring | string[] | Document | '*'Fields to return in the replaced document.
filterFilterInput<T>Additional filter criteria for the replace.

UpdateOneOptions

Extends mongodb.FindOneAndUpdateOptions (minus projection, returnDocument, includeResultMetadata).

OptionTypeDescription
projectionstring | string[] | Document | '*'Fields to return in the updated document.
filterFilterInput<T>Additional filter criteria for the update.
initArrayFieldsstring[]Array field names to initialize as [] if they don't exist before the update.

UpdateManyOptions

Extends mongodb.UpdateOptions (minus upsert).

OptionTypeDescription
filterFilterInput<T>Filter criteria — only matching documents are updated.
initArrayFieldsstring[]Array field names to initialize as [] if they don't exist before the update.