Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • MongoDB Documentation21টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • কেন Mongoose?
  • Installation
  • Connection
  • Schema
  • Model
  • Document
  • CRUD
  • Querying
  • Validation
  • Middleware
  • Population
  • Aggregation
  • Virtuals
  • Indexes
  • Error Handling
  • Update Operators
  • Transactions
  • Express Integration
  • Blog Project
  • Best Practices
  1. Docs
  2. MongoDB
  3. Mongoose Best Practices
MongoDBAdvanced

Mongoose Best Practices

প্রোডাকশন-ready Mongoose: connection reuse, lean(), naming, N+1 এড়ানো, security — বাংলায়।

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

১. Connection manage করুন

প্রতিটি request/function-এ নতুন connection খুলবেন না। একটা connection pool সারাজীবন activate রাখুন — serverless-এ global cache করুন (Next.js অধ্যায়টা দেখুন)।

২. lean() ব্যবহার

read-এ leanjs
js
// শুধু read/render করলে document method দরকার হয় না
const users = await User.find().lean();

// মাঝে document method (save, virtual) দরকার হলে lean NOT
const user = await User.findById(id);
user.name = "নতুন"; // document-এ modify
await user.save();

৩. N+1 এড়ান

  • লুপে find() → বদলে parallel/Promise.all বা এক query-তে $in
  • Reference-গুলো populate একবারে
  • select() দিয়ে অযথা field-না
  • Page-wise limit/skip — limitless নয়

৪. Naming নিয়ম

নিয়মউদাহরণ
Model name — Singular, PascalCaseUser, BlogPost
Collection — plural (default)users, blogposts
Field — camelCasefirstName, createdAt
Schema কনভেনশনtimestamps: true

৫. Security

  • .env-এ secrets — NEXT_PUBLIC_ দিয়ে client-এ পাঠাবেন না
  • findByIdAndUpdate-এ runValidators: true
  • Query injection ঠেকাতে input validate করুন — user-controlled raw query object-কে সরাসরি filter হিসেবে ব্যবহার করবেন না
  • Password/email-এ index + bcrypt hash
  • API limit-এ $lookup/aggregation-সহ complex query-র ট্র্যাক

৬. Mongoose + TypeScript

Typed model (v8)ts
ts
import { Schema, model, models, type Model } from "mongoose";

interface IUser {
  name: string;
  email: string;
  age?: number;
}

const userSchema = new Schema<IUser>({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  age: Number,
});

// hot reload গার্ড
const User: Model<IUser> =
  models.User || model<IUser>("User", userSchema);

export default User;
আরেকটি গুরুত্বপূর্ণ

Error message ও UI-তে Bangla ব্যবহার করতে পারেন, কিন্তু field/mongoose naming বাংলা না — MongoDB ও JS-এর সাথে সামঞ্জস্যপূর্ণ ASCII name রাখুন। এতে aggregate/$project-সহ সব জায়গায় কম সমস্যা হয়।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরReal Project: BlogMongoDB ডকুমেন্টেশন হোম
এই পেজে
Tech Tutor

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

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

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

mdanamolhasan1998@gmail.com