ما هو Mongoose؟
مكتبة Node.js (ODM) توفّر مخطّطات ونماذج وتحقّقًا فوق MongoDB.
npm install mongoose
الاتصال
import mongoose from "mongoose";
await mongoose.connect(process.env.MONGODB_URI);
تعريف مخطّط ونموذج
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, unique: true },
age: { type: Number, min: 0 },
createdAt: { type: Date, default: Date.now }
});
const User = mongoose.model("User", userSchema);
إنشاء مستند
const user = new User({ name: "براء", email: "b@x.com" });
await user.save();
// أو مباشرة
await User.create({ name: "سارة", email: "s@x.com" });
💡 Mongoose يضيف بنية وتحقّقًا ودوال مساعدة فوق مرونة MongoDB — الخيار الأشهر مع Node.js.
🎯 التالي: استعلامات Mongoose.