Loading Depth Study
Preparing your workspace
MongoDB queries use filter objects and operators to select documents that match specific conditions.
Simple queries can match exact field values, while operators provide more control. Common comparison operators include $gt, $gte, $lt, $lte, $eq, and $ne. Logical operators such as $and, $or, and $not help combine conditions. Query results can also be sorted, limited, and projected to return only required fields. Good queries are designed around the data the application needs rather than retrieving everything and filtering it in application code.
A learning dashboard might retrieve published courses where difficulty is Intermediate and sort the newest courses first.
const courses = await db.collection("courses")
.find({
published: true,
difficulty: "Intermediate",
duration: { $gte: 20 }
})
.sort({ createdAt: -1 })
.limit(10)
.toArray();
console.log(courses);The query returns up to ten published intermediate courses with at least twenty minutes of duration, sorted by newest first.