Queries, projections and arrays
A query is a document-shaped filter; a projection controls what comes back.
What you will be able to do
- Use projection { title: 1, minutes: 1, _id: 0 } for list endpoints.
- Use $elemMatch when multiple conditions must match the same array element.
- Explain a query before assuming it is fast.
In plain English
MongoDB filters use field conditions and operators such as $gt, $in and $elemMatch. Projections reduce network and memory cost. Query arrays with intent: $elemMatch requires one element to satisfy all conditions.
db.lessons.find(
{
published: true,
minutes: { $gte: 10, $lte: 20 },
tags: { $in: ["javascript", "typescript"] }
},
{ _id: 0, slug: 1, title: 1, minutes: 1 }
).sort({ minutes: -1 }).limit(10)Your turn
MediumFind short lessons
Find published lessons between 8 and 14 minutes and return only slug and title.
JAVASCRIPT · starter
db.lessons.find({})Check your understanding before moving on.
Knowledge checkQuestion 1
Which operator requires one array element to satisfy several conditions?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.