Skip to main content

MongoNestedService

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

Hierarchy: ServiceBase → MongoService → MongoNestedService


Constructor​

new MongoNestedService<T>(
dataType: Type | string,
fieldName: string,
options?: MongoNestedService.Options,
)
ArgumentTypeDescription
dataTypeType | stringThe parent OPRA data type or its registered name
fieldNamestringThe array field name within the parent document
optionsMongoNestedService.OptionsSee Interfaces below

Properties​

PropertyTypeDescription
fieldNamestringThe array field name in the parent document
nestedKeystringThe primary key field of array elements
defaultLimitnumberDefault findMany page size
nestedFilterFilterInput | functionCommon filter for nested operations

Methods​

All methods take documentId as the first argument. Methods that target a specific element also take nestedId as the second argument.

assert​

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

Throws ResourceNotAvailableError if the nested element does not exist.

options — ExistsOptions<T>


create​

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

Pushes an element into the array field and returns it.

options — CreateOptions


createOnly​

createOnly(documentId: AnyId, input: DTO<T>, options?: CreateOptions): Promise<PartialDTO<T>>

Pushes an element into the array field without fetching it back.

options — CreateOptions


count​

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

Returns the number of array elements matching the filter.

options — CountOptions<T>


delete​

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

Removes the element with the given nestedId. Returns 1 if deleted, 0 otherwise.

options — DeleteOptions<T>


deleteMany​

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

Removes all array elements matching the filter. Returns the number of deleted elements.

options — DeleteManyOptions<T>


exists​

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

Returns true if an element with the given nestedId exists.

options — ExistsOptions<T>


existsOne​

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

Returns true if at least one element matches the filter.

options — ExistsOptions<T>


findById​

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

Fetches an element by its nestedId. Returns undefined if not found.

options — FindOneOptions<T>


findOne​

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

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

options — FindOneOptions<T>


findMany​

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

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

options — FindManyOptions<T>


findManyWithCount​

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

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

options — FindManyOptions<T>


get​

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

Fetches an element by its nestedId. Throws ResourceNotAvailableError if not found.

options — FindOneOptions<T>


update​

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

Applies a partial update to the element with the given nestedId and returns the updated element.

options — UpdateOneOptions<T>


updateOnly​

updateOnly(documentId: AnyId, nestedId: AnyId, input: MongoPatchDTO<T>, options?: UpdateOneOptions<T>): Promise<number>

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

options — UpdateOneOptions<T>


updateMany​

updateMany(documentId: AnyId, input: MongoPatchDTO<T>, options?: UpdateManyOptions<T>): Promise<number>

Applies a partial update to all array elements matching the filter. Returns the number of affected elements.

options — UpdateManyOptions<T>


Lifecycle hooks​

Override in subclasses. Base implementations are no-ops.

HookSignature
_beforeCreate(command)(CreateCommand<T>) => Promise<void>
_afterCreate(command, result)(CreateCommand<T>, PartialDTO<T>) => Promise<void>
_beforeUpdate(command)(UpdateOneCommand<T>) => Promise<void>
_afterUpdate(command, result)(UpdateOneCommand<T>, PartialDTO<T> | undefined) => Promise<void>
_beforeUpdateMany(command)(UpdateManyCommand<T>) => Promise<void>
_afterUpdateMany(command, affected)(UpdateManyCommand<T>, number) => Promise<void>
_beforeDelete(command)(DeleteCommand<T>) => Promise<void>
_afterDelete(command, affected)(DeleteCommand<T>, number) => Promise<void>
_beforeDeleteMany(command)(DeleteCommand<T>) => Promise<void>
_afterDeleteMany(command, affected)(DeleteCommand<T>, number) => Promise<void>

Interfaces​

MongoNestedService.Options​

Extends MongoService.Options.

OptionTypeDefaultDescription
defaultLimitnumber10Default maximum number of elements returned by findMany
nestedKeystring'_id'Primary key field name within the nested array elements
nestedFilterFilterInput | ((command, _this) => FilterInput)—Common filter applied to every nested read and write operation

CreateOptions​

Extends mongodb.InsertOneOptions.

OptionTypeDescription
projectionstring | string[] | Document | '*'Fields to return in the fetched-back element.

CountOptions​

Extends mongodb.CountOptions.

OptionTypeDescription
filterFilterInput<T>Filter criteria applied to the nested elements.
documentFilterFilterInputAdditional filter applied to the parent document.

DeleteOptions​

Extends mongodb.DeleteOptions.

OptionTypeDescription
filterFilterInput<T>Additional filter for the element to delete.
documentFilterFilterInputAdditional filter applied to the parent document.

DeleteManyOptions​

Extends mongodb.DeleteOptions.

OptionTypeDescription
filterFilterInput<T>Filter criteria — only matching elements are deleted.
documentFilterFilterInputAdditional filter applied to the parent document.

ExistsOptions​

Extends mongodb.CommandOperationOptions (minus session).

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

FindOneOptions​

Extends mongodb.AggregateOptions.

OptionTypeDescription
filterFilterInput<T>Additional filter criteria.
projectionstring | string[] | Document | '*'Fields to include. '*' returns all fields.
sortstring[]Sort directives.
skipnumberNumber of elements to skip.
preStagesmongodb.Document[]Aggregation stages inserted before the filter stage.
postStagesmongodb.Document[]Aggregation stages appended after other stages.
documentFilterFilterInputAdditional filter applied to the parent document.

FindManyOptions​

Extends mongodb.AggregateOptions.

OptionTypeDescription
filterFilterInput<T>Filter criteria for the nested elements.
projectionstring | string[] | Document | '*'Fields to include. '*' returns all fields.
sortstring[]Sort directives.
limitnumberMaximum number of elements to return. Defaults to defaultLimit.
skipnumberNumber of elements to skip.
preStagesmongodb.Document[]Aggregation stages inserted before the filter stage.
postStagesmongodb.Document[]Aggregation stages appended after the limit stage.
documentFilterFilterInputAdditional filter applied to the parent document.
nestedFilterFilterInputPer-call filter applied to nested elements, merged with the service-level nestedFilter.

UpdateOneOptions​

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

OptionTypeDescription
projectionstring | string[] | Document | '*'Fields to return in the updated element.
filterFilterInput<T>Additional filter criteria for the update.
documentFilterFilterInputAdditional filter applied to the parent document.
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 elements are updated.
documentFilterFilterInputAdditional filter applied to the parent document.
initArrayFieldsstring[]Array field names to initialize as [] if they don't exist before the update.