Registry / database / nest-mongodb

nest-mongodb

JSON →
library6.4.0jsnpmunverified

A NestJS module for integrating the MongoDB driver (not Mongoose) into NestJS applications. Current version 6.4.0 supports NestJS 6-9 and MongoDB 3-4. Provides InjectDb and InjectCollection decorators for injecting native Mongo Db and Collection instances. Released monthly, modeled after official NestJS Mongoose module but without ODM layer. Key differentiator: direct MongoDB driver access without Mongoose overhead.

npm install nest-mongodb
INSTALL
IMPORT
SIG · NEST-MONGODB
N
nest-mongodb
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')
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(); } }
Debug
Known issues
gotchaMissing useNewUrlParser or useUnifiedTopology options may cause connection warnings in MongoDB driver 3.x
fix
Pass { useNewUrlParser: true, useUnifiedTopology: true } as third argument to forRoot()
affects: >=3.0.0 <4.0.0
deprecatedResult.insertedCount and result.ops are deprecated in MongoDB driver 4.0+
fix
Use result.acknowledged and result.insertedId instead
affects: >=4.0.0
breakingMongoDB driver v4 changed collection methods async behavior
fix
Update methods to use new API (e.g., insertOne returns InsertOneResult)
affects: >=4.0.0 <5.0.0
gotchaInjection of Collection via @InjectCollection('name') uses string token, not class token
fix
Use @InjectCollection('collectionName') exactly matching the string passed to forFeature(['collectionName'])
affects: >=1.0.0
deprecatedUse of useClass with MongoOptionsFactory requires explicit instantiation
fix
Use useFactory instead for simpler configuration
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'mongodb' or its corresponding type declarations.
Missing mongodb dependency or Mongoose installed instead
fix
npm install mongodb
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
fix
Ensure 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
fix
Add 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
fix
Add @InjectDb() decorator to constructor parameter
Class constructor MongoModule cannot be invoked without 'new'
Using CommonJS require in TypeScript/esModuleInterop false
fix
Use import instead of require, or set esModuleInterop: true in tsconfig.json
Upgrade
Version history
6.4.0latest on npm
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
Agent activity
10 hits · last 30 days
node
8
Resources
nest-mongodb — npm install nest-mongodb · libregistry