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')
ESM import is standard. CJS require works in CommonJS projects but not recommended for TypeScript.
InjectDb
✓ import { InjectDb } from 'nest-mongodb'
✗ import { InjectDb } from 'nest-mongodb/lib'
Always import from top-level 'nest-mongodb'. Subpath imports are not part of public API.
InjectCollection
✓ import { InjectCollection } from 'nest-mongodb'
✗ import { InjectCollection } from '@nestjs/mongoose'
Common mistake: confusing with Mongoose module. nest-mongodb uses native MongoDB driver, not Mongoose.
MongoOptionsFactory
✓ import { MongoOptionsFactory } from 'nest-mongodb'
✗ import { MongoOptionsFactory } from 'nest-mongodb/interfaces'
Interface is exported from main module, not a subpath.
Shows basic setup with MongoModule.forRoot(), Db injection via @InjectDb(), and executing a query. Uses environment variables for configuration.
import { Module } from '@nestjs/common';
import { MongoModule } from 'nest-mongodb';
@Module({
imports: [MongoModule.forRoot(
process.env.MONGO_URI ?? 'mongodb://localhost:27017',
process.env.MONGO_DB_NAME ?? 'test',
{ useNewUrlParser: true, useUnifiedTopology: true }
)]
})
export class AppModule {}
// In a service:
import { Injectable, Inject } from '@nestjs/common';
import { InjectDb } from 'nest-mongodb';
import { Db } from 'mongodb';
@Injectable()
export class CatsService {
constructor(@InjectDb() private readonly db: Db) {}
async getCats() {
return this.db.collection('cats').find().toArray();
}
}
Errors
Common errors & fixes
Cannot find module 'mongodb' or its corresponding type declarations.
Missing mongodb dependency or Mongoose installed instead
Nest can't resolve dependencies of CatsService (?). Please make sure that the argument Db at index [0] is available in the AppModule context.
Db provider not registered because MongoModule not imported
fixEnsure MongoModule.forRoot() is added to imports array of module that provides the service
Collection 'cats' not found. Did you forget to call MongoModule.forFeature(['cats'])?
forFeature not called for the collection used in @InjectCollection
fixAdd MongoModule.forFeature(['cats']) to the module imports where the collection is injected
TypeError: Cannot read properties of undefined (reading 'collection')
Db object not injected properly due to missing @InjectDb() decorator
fixAdd @InjectDb() decorator to constructor parameter
Class constructor MongoModule cannot be invoked without 'new'
Using CommonJS require in TypeScript/esModuleInterop false
fixUse import instead of require, or set esModuleInterop: true in tsconfig.json
Audit
Dependencies
@nestjs/commonrequiredPeer dependency for NestJS decorators and module system
@nestjs/corerequiredPeer dependency for NestJS core functionality
mongodbrequiredPeer dependency for MongoDB driver types and implementation
reflect-metadatarequiredPeer dependency required for NestJS decorators
rxjsrequiredPeer dependency for reactive programming support in NestJS