Registry / database / nest-mongodb-ts5

nest-mongodb-ts5

JSON →
library6.4.0jsnpmunverified

A NestJS module that integrates the official MongoDB Node.js driver (not Mongoose) into NestJS applications, providing dependency injection for Db and Collection objects. Version 6.4.0 supports NextJS 6-10 and TypeScript 5 decorators. This is a fork of the original erikc5000/nest-mongodb, updated to support TypeScript 5 decorator syntax, which the original maintainer has not yet published. Differentiates from the Mongoose module by offering lower-level MongoDB driver access without schema/models.

npm install nest-mongodb-ts5
INSTALL
IMPORT
SIG · NEST-MONGODB-TS5
N
nest-mongodb-ts5
databasejavascriptv6.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MongoModule
import { MongoModule } from 'nest-mongodb'
const MongoModule = require('nest-mongodb').MongoModule
ESM-only since v6. Use named import from the package.
InjectDb
import { InjectDb } from 'nest-mongodb'
import { InjectDb } from 'nest-mongodb/decorators'
Symbol is exported from package root, not subpath.
InjectCollection
import { InjectCollection } from 'nest-mongodb'
import { InjectCollection } from 'nest-mongodb/lib/decorators'
Direct export from package root.

Shows async configuration with MongoModule.forRootAsync and usage of @InjectDb decorator to inject the MongoDB Db instance.

import { Module } from '@nestjs/common'; import { MongoModule } from 'nest-mongodb'; @Module({ imports: [ MongoModule.forRootAsync({ useFactory: () => ({ uri: process.env.MONGO_URI ?? 'mongodb://localhost:27017', dbName: process.env.MONGO_DB ?? 'test', }), }), ], }) export class AppModule {} // service.ts import { Injectable } from '@nestjs/common'; import { InjectDb } from 'nest-mongodb'; import { Db } from 'mongodb'; @Injectable() export class MyService { constructor(@InjectDb() private readonly db: Db) {} async getCollections() { const collections = await this.db.listCollections().toArray(); return collections; } }
Debug
Known issues
breakingTypeScript 5 decorator syntax is required; older TypeScript versions may break.
fix
Upgrade TypeScript to >=5.0.0 and ensure experimentalDecorators is false.
affects: >=6.4.0
gotchaMongoModule.forRoot() expects (uri: string, dbName: string, options?: MongoClientOptions) – options object is third argument.
fix
Pass options as third parameter, not as part of uri string.
affects: >=6.0.0
gotchaInjectCollection('collectionName') requires collection name as argument; omitting it injects undefined.
fix
Always pass a non-empty string to @InjectCollection().
affects: >=6.0.0
deprecatedMongoModule.forRoot() synchronous configuration is deprecated in favor of async pattern.
fix
Use MongoModule.forRootAsync() for dynamic configuration.
affects: >=6.0.0
gotchaimport { Db } from 'mongodb' – not 'mongodb/db' or other subpaths.
fix
Import Db from 'mongodb' top-level package.
affects: >=6.0.0
Errors
Common errors & fixes
Cannot find module 'nest-mongodb'
Package not installed or version mismatch.
fix
npm install nest-mongodb mongodb
TypeError: Class extends value undefined is not a function
Decorator metadata not available (reflect-metadata not imported).
fix
Add 'import 'reflect-metadata'' at top of main entry file.
NullInjectorError: No provider for Db!
MongoModule not imported in root module or Db not provided correctly.
fix
Ensure MongoModule.forRoot() or forRootAsync() is called in a module imported by the root module.
Property 'insertedCount' does not exist on type 'InsertOneResult'
MongoDB driver v6 changed return types; insertedCount is removed.
fix
Use result.acknowledged to check success and result.insertedId for inserted ID.
Cannot use import statement outside a module
Package is ESM-only; CommonJS not supported.
fix
Set 'type': 'module' in package.json or use dynamic import().
Upgrade
Version history
6.4.0latest on npm
Audit
Dependencies
@nestjs/commonrequiredRequires NestJS decorators and module system
@nestjs/corerequiredRequired for NestJS application bootstrapping
mongodbrequiredMongoDB driver used for database operations
reflect-metadatarequiredRequired for decorator metadata reflection
rxjsrequiredUsed by NestJS for reactive programming
Agent activity
2 hits · last 30 days
node
2
Resources
nest-mongodb-ts5 — npm install nest-mongodb-ts5 · libregistry