0Pricing
Node.js Backend Development Bootcamp · درس

الاستعلام عن البيانات وتصفيتها باستخدام Mongoose

تجاوز عمليات CRUD الأساسية وتعلّم كيفية إنشاء استعلامات قوية في Mongoose باستخدام عوامل التصفية والمعاملات والفرز وتقسيم الصفحات والإسقاطات.

الاستعلام عن البيانات وتصفيتها باستخدام Mongoose درس مجاني في Node.js Backend Development Bootcamp على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Node.js Backend Development Bootcamp، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Beyond Find-All

Fetching every document with Model.find() works for tiny collections, but real apps need to filter, sort, limit, and shape their results.

Mongoose gives you a fluent query API that mirrors MongoDB's query language.

const users = await User.find({ active: true });

Filtering by Field

Pass an object to find() to match documents by exact field values. Multiple keys are combined with logical AND.

const admins = await User.find({ role: 'admin', active: true });

Comparison Operators

MongoDB operators start with $. The most common comparisons are:

  • $gt / $gte greater than (or equal)
  • $lt / $lte less than (or equal)
  • $ne not equal
const adults = await User.find({ age: { $gte: 18 } });

Matching Multiple Values

Use $in to match any value from a list, and $nin to exclude a list. This is cleaner than chaining many OR conditions.

const team = await User.find({ role: { $in: ['admin', 'editor'] } });

Logical OR

The $or operator takes an array of conditions; a document matches if any one is true.

const results = await User.find({
  $or: [{ city: 'Berlin' }, { city: 'Paris' }]
});

Sorting Results

Chain .sort() to order results. Use 1 for ascending and -1 for descending. You can sort by multiple fields.

const recent = await User.find().sort({ createdAt: -1, name: 1 });

Limiting and Skipping

Pagination relies on .limit() (max documents) and .skip() (how many to bypass). For page 2 with 10 per page, skip 10 and limit 10.

const page = 2, perPage = 10;
const items = await User.find()
  .skip((page - 1) * perPage)
  .limit(perPage);

Selecting Specific Fields

Use .select() (projection) to return only the fields you need. Prefix a field with - to exclude it. This reduces payload size and hides sensitive data.

const safe = await User.find().select('name email -_id');
const noPass = await User.find().select('-password');

Finding a Single Document

findOne() returns the first match or null. findById() is a shortcut for matching by _id.

const user = await User.findOne({ email: 'a@b.com' });
const byId = await User.findById('652f1c...');

Counting Documents

To know how many documents match a filter without fetching them all, use countDocuments(). This is essential for showing total pages in pagination.

const total = await User.countDocuments({ active: true });
console.log('Active users:', total);

Text & Regex Search

For partial matches, use a regular expression. The $regex operator with the i option makes a case-insensitive search.

const matches = await User.find({
  name: { $regex: 'an', $options: 'i' }
});

Quick Check

Test your Mongoose querying skills.

Recap

You can now build rich Mongoose queries:

  • Filter by field and combine with operators like $gte, $in, $or
  • Order results with .sort()
  • Paginate with .skip() and .limit()
  • Shape output with .select()
  • Count matches with countDocuments()

These tools turn a basic data layer into a flexible, production-ready API.

الأسئلة الشائعة

هل درس «الاستعلام عن البيانات وتصفيتها باستخدام Mongoose» مجاني؟

نعم — نص درس «الاستعلام عن البيانات وتصفيتها باستخدام Mongoose» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Node.js Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.

ماذا ستتعلم في «الاستعلام عن البيانات وتصفيتها باستخدام Mongoose»؟

تجاوز عمليات CRUD الأساسية وتعلّم كيفية إنشاء استعلامات قوية في Mongoose باستخدام عوامل التصفية والمعاملات والفرز وتقسيم الصفحات والإسقاطات. تتمرن على Node.js Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Node.js Backend Development Bootcamp؟

لا تُشترط خبرة سابقة. Node.js Backend Development Bootcamp على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «الاستعلام عن البيانات وتصفيتها باستخدام Mongoose»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Node.js Backend Development Bootcamp هذا؟

نعم. كل درس في Node.js Backend Development Bootcamp يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ربط Node.js بـ MongoDB
  2. Mongoose ODM لنمذجة البيانات
  3. عمليات CRUD باستخدام Mongoose
  4. الاستعلام عن البيانات وتصفيتها باستخدام Mongoose
← العودة إلى Node.js Backend Development Bootcamp