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. Schema Validation
MongoDBIntermediate

Schema Validation

Built-in ও custom validator দিয়ে ডেটা যাচাই: required, enum, min/max, match, unique — বাংলায়।

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

Validation schema-তে save() বা validate()-এর সময় চলে — অবৈধ ডেটা collection-এ ঢোকার আগেই আটকে যায়।

Built-in validators

Common validatorsjs
js
const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 3,
    maxlength: 50,
  },
  email: {
    type: String,
    required: true,
    lowercase: true,
    match: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
  },
  age: {
    type: Number,
    min: 13,
    max: 120,
  },
  role: {
    type: String,
    enum: ["user", "admin", "moderator"],
    default: "user",
  },
  profileUrl: {
    type: String,
    validate: {
      validator: (v) => v.startsWith("https://"),
      message: "URL-টি https:// দিয়ে শুরু হোক",
    },
  },
});

Custom validator

Custom validationjs
js
const orderSchema = new mongoose.Schema({
  quantity: {
    type: Number,
    validate: {
      validator: (value) => Number.isInteger(value) && value > 0,
      message: (props) => props.value + " একটি বৈধ quantity নয়",
    },
  },
  discountCode: {
    type: String,
    validate: {
      validator: async (value) => {
        // ডেটাবেসে code-টা আছে কিনা যাচাই
        const code = await Coupon.findOne({ code: value });
        return code !== null;
      },
      message: "Invalid discount code",
    },
  },
});

unique — validator নয়

unique গুরুত্বপূর্ণ বিষয়

unique: true validator নয় — এটি একটা unique index। ফলে apply হতে collection-এ অবশ্যই index থাকতে হবে, আর duplicate হলে E11000 MongoServerError হয় (ValidationError নয়)। Error handle করার সময় code === 11000 চেক করুন।

Validation error handle

ValidationErrorjs
js
try {
  await User.create({ name: "ab", email: "not-an-email" });
} catch (err) {
  if (err.name === "ValidationError") {
    for (const field in err.errors) {
      console.log(field, "→", err.errors[field].message);
    }
  }
}
শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরQuery Builder ও OperatorsপরেরMiddleware (pre/post hooks)
এই পেজে
Tech Tutor

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

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

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

mdanamolhasan1998@gmail.com