Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • NestJS Documentation21টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • Installation
  • Modules
  • Controllers
  • Providers & DI
  • Pipes
  • Guards
  • Interceptors
  • Exception Handling
  • Middleware
  • Database
  • Authentication
  • Config & Env
  • File Upload
  • WebSockets
  • Microservices
  • Swagger
  • Testing
  • Best Practices
  • Project
  • Interview
  1. Docs
  2. NestJS
  3. NestJS + MongoDB (Mongoose)
NestJSAdvanced

NestJS + MongoDB (Mongoose)

@nestjs/mongoose, Schema, Model, repository pattern, CRUD service — বাংলায়।

11টি অংশবাংলা ভাষায়

NestJS-এর জন্য অফিসিয়াল @nestjs/mongoose wrapper আছে। এখানে Schema + Model দিয়ে database পরিষ্কারভাবে manage করা যায়।

Setup

npm i @nestjs/mongoose mongoose
npm i -D @types/mongoose
src/app.module.tstypescript
typescript
import { Module } from "@nestjs/common";
import { MongooseModule } from "@nestjs/mongoose";

@Module({
  imports: [
    MongooseModule.forRoot("mongodb://localhost:27017/nestapp"),
    // বা env থেকে
    // MongooseModule.forRoot(process.env.MONGO_URI),
  ],
})
export class AppModule {}

Schema Definition

src/users/schemas/user.schema.tstypescript
typescript
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { HydratedDocument } from "mongoose";

export type UserDocument = HydratedDocument<User>;

@Schema({ timestamps: true })
export class User {
  @Prop({ required: true, trim: true })
  name: string;

  @Prop({ required: true, unique: true, lowercase: true })
  email: string;

  @Prop({ required: true, select: false }) // find-এ exclude
  password: string;

  @Prop({ default: "user" })
  role: string;
}

export const UserSchema = SchemaFactory.createForClass(User);

Module & Inject Model

src/users/users.module.tstypescript
typescript
import { Module } from "@nestjs/common";
import { MongooseModule } from "@nestjs/mongoose";
import { UsersService } from "./users.service";
import { UsersController } from "./users.controller";
import { User, UserSchema } from "./schemas/user.schema";

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: User.name, schema: UserSchema },
    ]),
  ],
  controllers: [UsersController],
  providers: [UsersService],
  exports: [UsersService],
})
export class UsersModule {}

Service (Repository pattern)

src/users/users.service.tstypescript
typescript
import { Injectable, NotFoundException } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Model } from "mongoose";
import { User, UserDocument } from "./schemas/user.schema";
import { CreateUserDto } from "./dto/create-user.dto";

@Injectable()
export class UsersService {
  constructor(
    @InjectModel(User.name) private userModel: Model<UserDocument>,
  ) {}

  async create(dto: CreateUserDto): Promise<User> {
    const created = new this.userModel(dto);
    return created.save();
  }

  async findAll(): Promise<User[]> {
    return this.userModel.find().exec();
  }

  async findOne(id: string): Promise<User> {
    const user = await this.userModel.findById(id).exec();
    if (!user) throw new NotFoundException("User পাওয়া যায়নি");
    return user;
  }

  async update(id: string, dto: Partial<CreateUserDto>): Promise<User> {
    const updated = await this.userModel
      .findByIdAndUpdate(id, dto, { new: true })
      .exec();
    if (!updated) throw new NotFoundException("User পাওয়া যায়নি");
    return updated;
  }

  async remove(id: string): Promise<void> {
    const deleted = await this.userModel.findByIdAndDelete(id).exec();
    if (!deleted) throw new NotFoundException("User পাওয়া যায়নি");
  }
}
DB Choice

PostgreSQL চাইলে TypeORM বা Prisma ব্যবহার করুন — NestJS-এ সবই প্রথম-class support পায়।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরNestJS MiddlewareপরেরNestJS Authentication (JWT)
এই পেজে
Tech Tutor

বাংলায় ওয়েব ডেভেলপমেন্ট ডকুমেন্টেশন শিখুন — ধাপে ধাপে।

Homeসব ডকুমেন্টেশনQuick Startআমার নোটস

© 2026 Tech Tutor. বাংলায় শিখুন, বাংলায় গড়ুন।

mdanamolhasan1998@gmail.com