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 (
nullmeans unset the field). - Nested
ComplexTypefields are themselvesMongoPatchDTO-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
_$pushand_$pulloperators for array mutations.
Patch operators
| Operator | Type | Description |
|---|---|---|
_$push | Partial<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);