MongoDB
@opra/mongodb provides three service base classes that cover the most common MongoDB data shapes. Each class handles codec generation, filter translation, projection, pagination, lifecycle hooks, and the for(context) per-request wiring — leaving your code focused on business logic.
npm install @opra/mongodb mongodb
Which service should I use?
| Your data shape | Use |
|---|---|
A named collection of records, each identified by _id | MongoCollectionService |
| A collection that always holds exactly one document | MongoSingletonService |
| An array field embedded inside a parent document | MongoNestedService |
All three share the same configuration, the same for(context) pattern, and the same lifecycle hook interface. Only the method signatures differ.
Service hierarchy
MongoService shared base: db, codec, documentFilter, interceptor
├── MongoEntityService low-level CRUD primitives + lifecycle hooks
│ ├── MongoCollectionService full CRUD on a named collection
│ └── MongoSingletonService single-document collection (no key argument)
└── MongoNestedService CRUD on an array field inside a parent document
Guides
- Getting Started — first service, database connection, NestJS wiring
- Querying — filter, projection, sort, pagination, aggregation stages
- Mutations — create, update, replace, delete; PatchDTO vs UpdateFilter
- Nested Arrays — managing embedded array elements with
MongoNestedService - Lifecycle Hooks — timestamps, validation, audit logging
- Interceptors & Transactions — cross-cutting logic and multi-document transactions