mongodb-typescript is a lightweight wrapper around the official MongoDB Node.js driver that hydrates plain MongoDB documents into TypeScript-defined class instances. Version 3.1.0 requires mongodb ^4.8.0 and reflect-metadata ^0.1.13 as peer dependencies. It uses class-transformer under the hood to map between plain objects and classes, preserving methods, getters, and nested structures. Unlike Mongoose, it does not enforce validation, schema stripping, or middleware. Key features include decorators (@id, @nested, @ref, @ignore), automatic hydration/dehydration, population of referenced collections, and schema index definitions. The package ships TypeScript types and is designed for developers who want strong typing without the overhead of a full ODM. Release cadence is irregular; latest update in 2022.
npm install mongodb-typescriptNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Connects to MongoDB, defines a User class with @id decorator, inserts a document, and retrieves it with methods intact.
Update mongodb to ^4.8.0 in package.json.
Replace repository.c with repository.collection.
Use import syntax or set "type": "module" in package.json, or use dynamic import.
Add @objectId decorator to non-primary ObjectId fields.
Ensure your entity class has exactly one field decorated with @id.
Add "experimentalDecorators": true and "emitDecoratorMetadata": true to tsconfig.json.
Use import syntax: import { Repository } from 'mongodb-typescript'Add import 'reflect-metadata' at the top of your entry file.
Run npm install mongodb-typescript and check import path.
Initialize the property with a default value: age: number = 15;