{"id":43409,"library":"nest-mongodb","title":"nest-mongodb","description":"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.","status":"active","version":"6.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/erikc5000/nest-mongodb","tags":["javascript","mongo","mongodb","nest","nestjs","typescript"],"install":[{"cmd":"npm install nest-mongodb","lang":"bash","label":"npm"},{"cmd":"yarn add nest-mongodb","lang":"bash","label":"yarn"},{"cmd":"pnpm add nest-mongodb","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for NestJS decorators and module system","package":"@nestjs/common","optional":false},{"reason":"Peer dependency for NestJS core functionality","package":"@nestjs/core","optional":false},{"reason":"Peer dependency for MongoDB driver types and implementation","package":"mongodb","optional":false},{"reason":"Peer dependency required for NestJS decorators","package":"reflect-metadata","optional":false},{"reason":"Peer dependency for reactive programming support in NestJS","package":"rxjs","optional":false}],"imports":[{"note":"ESM import is standard. CJS require works in CommonJS projects but not recommended for TypeScript.","wrong":"const MongoModule = require('nest-mongodb')","symbol":"MongoModule","correct":"import { MongoModule } from 'nest-mongodb'"},{"note":"Always import from top-level 'nest-mongodb'. Subpath imports are not part of public API.","wrong":"import { InjectDb } from 'nest-mongodb/lib'","symbol":"InjectDb","correct":"import { InjectDb } from 'nest-mongodb'"},{"note":"Common mistake: confusing with Mongoose module. nest-mongodb uses native MongoDB driver, not Mongoose.","wrong":"import { InjectCollection } from '@nestjs/mongoose'","symbol":"InjectCollection","correct":"import { InjectCollection } from 'nest-mongodb'"},{"note":"Interface is exported from main module, not a subpath.","wrong":"import { MongoOptionsFactory } from 'nest-mongodb/interfaces'","symbol":"MongoOptionsFactory","correct":"import { MongoOptionsFactory } from 'nest-mongodb'"}],"quickstart":{"code":"import { Module } from '@nestjs/common';\nimport { MongoModule } from 'nest-mongodb';\n\n@Module({\n  imports: [MongoModule.forRoot(\n    process.env.MONGO_URI ?? 'mongodb://localhost:27017',\n    process.env.MONGO_DB_NAME ?? 'test',\n    { useNewUrlParser: true, useUnifiedTopology: true }\n  )]\n})\nexport class AppModule {}\n\n// In a service:\nimport { Injectable, Inject } from '@nestjs/common';\nimport { InjectDb } from 'nest-mongodb';\nimport { Db } from 'mongodb';\n\n@Injectable()\nexport class CatsService {\n  constructor(@InjectDb() private readonly db: Db) {}\n\n  async getCats() {\n    return this.db.collection('cats').find().toArray();\n  }\n}","lang":"typescript","description":"Shows basic setup with MongoModule.forRoot(), Db injection via @InjectDb(), and executing a query. Uses environment variables for configuration."},"warnings":[{"fix":"Pass { useNewUrlParser: true, useUnifiedTopology: true } as third argument to forRoot()","message":"Missing useNewUrlParser or useUnifiedTopology options may cause connection warnings in MongoDB driver 3.x","severity":"gotcha","affected_versions":">=3.0.0 <4.0.0"},{"fix":"Use result.acknowledged and result.insertedId instead","message":"Result.insertedCount and result.ops are deprecated in MongoDB driver 4.0+","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Update methods to use new API (e.g., insertOne returns InsertOneResult)","message":"MongoDB driver v4 changed collection methods async behavior","severity":"breaking","affected_versions":">=4.0.0 <5.0.0"},{"fix":"Use @InjectCollection('collectionName') exactly matching the string passed to forFeature(['collectionName'])","message":"Injection of Collection via @InjectCollection('name') uses string token, not class token","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use useFactory instead for simpler configuration","message":"Use of useClass with MongoOptionsFactory requires explicit instantiation","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'-4':27 '-9':23 '3':26 '6':22 '6.4.0':19 'access':58 'applic':16 'collect':39 'current':17 'db':37 'decor':32 'differenti':54 'direct':55 'driver':11,57 'inject':34 'injectcollect':31 'injectdb':29 'instanc':40 'integr':8 'javascript':62 'key':53 'layer':52 'model':43 'modul':6,48 'mongo':36,63 'mongodb':3,10,25,56,64 'mongoos':13,47,60 'month':42 'nativ':35 'nest':2,65 'nest-mongodb':1 'nestj':5,15,21,46,66 'odm':51 'offici':45 'overhead':61 'provid':28 'releas':41 'support':20 'typescript':67 'version':18 'without':50,59","created_at":"2026-06-05T16:59:39.717786+00:00","updated_at":"2026-06-05T16:59:39.717786+00:00","problems":[{"fix":"npm install mongodb","cause":"Missing mongodb dependency or Mongoose installed instead","error":"Cannot find module 'mongodb' or its corresponding type declarations."},{"fix":"Ensure MongoModule.forRoot() is added to imports array of module that provides the service","cause":"Db provider not registered because MongoModule not imported","error":"Nest can't resolve dependencies of CatsService (?). Please make sure that the argument Db at index [0] is available in the AppModule context."},{"fix":"Add MongoModule.forFeature(['cats']) to the module imports where the collection is injected","cause":"forFeature not called for the collection used in @InjectCollection","error":"Collection 'cats' not found. Did you forget to call MongoModule.forFeature(['cats'])?"},{"fix":"Add @InjectDb() decorator to constructor parameter","cause":"Db object not injected properly due to missing @InjectDb() decorator","error":"TypeError: Cannot read properties of undefined (reading 'collection')"},{"fix":"Use import instead of require, or set esModuleInterop: true in tsconfig.json","cause":"Using CommonJS require in TypeScript/esModuleInterop false","error":"Class constructor MongoModule cannot be invoked without 'new'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/erikc5000/nest-mongodb#readme","github":"https://github.com/erikc5000/nest-mongodb","docs":null,"changelog":null,"pypi":null,"npm":"nest-mongodb","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}