Skip to main content

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 shapeUse
A named collection of records, each identified by _idMongoCollectionService
A collection that always holds exactly one documentMongoSingletonService
An array field embedded inside a parent documentMongoNestedService

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