Skip to main content

Types

Utility types exported from @opra/mongodb.

import type { MongoPatchDTO } from '@opra/mongodb';

MongoPatchDTO<T>

type MongoPatchDTO<T> = _MongoPatchDTO<DTO<T>> & PatchOperators<T>

A MongoDB-aware patch DTO. It is a recursive partial of T where:

  • Every field is optional and nullable (null means unset the field).
  • Nested ComplexType fields are themselves MongoPatchDTO-shaped, so only changed sub-fields need to be provided.
  • Array fields accept an array of partial elements.
  • The top-level object (and any nested object) may include _$push and _$pull operators for array mutations.

Patch operators

OperatorTypeDescription
_$pushPartial<PickArrays<T>>Appends elements to array fields. Key is the array field name, value is the element(s) to push.
_$pull{ [K in ArrayKeys<T>]?: (string | number | boolean)[] }Removes elements from array fields by value.

Example

const patch: MongoPatchDTO<Customer> = {
name: 'Alice Smith', // $set: { name: 'Alice Smith' }
phone: null, // $unset: { phone: 1 }
address: {
city: 'Springfield', // $set: { 'address.city': 'Springfield' }
},
_$push: {
tags: 'vip', // $push: { tags: 'vip' }
},
_$pull: {
tags: ['trial'], // $pull: { tags: { $in: ['trial'] } }
},
};

await service.update(id, patch);