💡 هذا الدرس يفترض أنك أنشأت Cluster وأعددت الشبكة والمستخدمين بالفعل — راجع Atlas والسحابة لخطوات البدء إن لم تكن قد فعلت. هنا نتعمّق في ميزات Atlas المتقدّمة: البحث، الأتمتة، والعمليات.
VPC Peering
لربط Atlas بشبكتك الافتراضية الخاصة بدل السماح بأي IP:
- Network Access -> Peering -> Add Peering Connection
- اختر provider (AWS/GCP/Azure)
- أدخل معرف VPC وحساب الـ provider
- اقبل الطلب من الجانب الآخر
Atlas Search
Atlas Search مبني على Lucene ويوفر بحثًا نصيًا متقدمًا.
إنشاء Index Search
// Atlas UI: Search -> Create Index
{
"mappings": {
"dynamic": false,
"fields": {
"title": {
"type": "string",
"analyzer": "lucene.arabic"
},
"description": {
"type": "string",
"analyzer": "lucene.standard"
},
"price": { "type": "number" },
"tags": { "type": "string", "multi": {
"analyzer": "lucene.keyword"
}},
"createdAt": { "type": "date" }
}
}
}
الاستعلام باستخدام $search
db.products.aggregate([
{ $search: {
index: "default",
compound: {
must: [
{ text: { query: "هاتف ذكي", path: "title", fuzzy: { maxEdits: 1 } }},
{ range: { path: "price", gte: 100, lte: 1000 }}
],
should: [
{ text: { query: "جديد", path: "tags" }}
]
}
}},
{ $limit: 20 },
{ $project: { title: 1, price: 1, score: { $meta: "searchScore" }}}
])
Autocomplete
{
"mappings": {
"dynamic": false,
"fields": {
"title": {
"type": "autocomplete",
"tokenizer": "lucene.edgengram",
"minGrams": 2,
"maxGrams": 15
}
}
}
}
db.products.aggregate([
{ $search: {
autocomplete: { query: "هات", path: "title" }
}},
{ $limit: 5 },
{ $project: { title: 1 }}
])
Atlas Triggers
Triggers تستجيب للأحداث في قاعدة البيانات (CRUD) أو جداول زمنية.
// Database Trigger — مثال: إرسال بريد عند تقديم طلب
exports = async function(changeEvent) {
const { fullDocument } = changeEvent;
const emailService = context.services.get("email-service");
await emailService.send({
to: fullDocument.customerEmail,
subject: "تم استلام طلبك",
body: `رقم الطلب: ${fullDocument._id}`
});
};
// Scheduled Trigger — كل ساعة
exports = async function() {
const db = context.services.get("main").db("store");
const expired = await db.collection("carts")
.updateMany(
{ updatedAt: { $lt: new Date(Date.now() - 24*60*60*1000) }},
{ $set: { status: "abandoned" }}
);
};
Serverless Instances
Atlas Serverless يتوسع تلقائيًا حسب الطلب، مناسب للتطبيقات ذات الحمولة المتغيرة.
# إنشاء serverless instance عبر Atlas CLI
atlas serverless create myServerless \
--provider AWS \
--region us-east-1
# أو عبر UI: Create -> Serverless
النسخ الاحتياطي والاستعادة
Backups التلقائية
# تفعيل النسخ الاحتياطي
atlas backups enable --clusterName myCluster
# استعادة نسخة
atlas backups restore \
--clusterName myCluster \
--snapshotId <snapshot_id>
Export البيانات
# Export باستخدام mongodump
mongodump --uri "mongodb+srv://<user>:<pass>@<cluster>.mongodb.net/<db>" \
--out ./backup-$(date +%Y%m%d)
# Import باستخدام mongorestore
mongorestore --uri "mongodb+srv://<user>:<pass>@<cluster>.mongodb.net/<db>" \
./backup-20250101
Monitoring والتنبيهات
# عرض metrics
atlas metrics describe myCluster --period PT1H
# إنشاء تنبيه
atlas alerts create \
--event "CPU_UTILIZATION_HIGH" \
--metricThreshold 80 \
--notificationType EMAIL \
--notificationEmail "admin@example.com"
تمارين
- أنشئ Atlas cluster مجاني وتصل إليه من تطبيق Node.js
- أضف Search index مع autocomplete للغة العربية
- أنشئ Database trigger يُسجّل جميع عمليات الحذف في مجموعة logs
- جدول مهمة cron لإرسال تقرير يومي
🎯 التالي: Change Streams والمراقبة اللحظية.