Section 2 · Reading data

find() — ask questions with a document

The query itself is a document: { field: value } means "match documents where that field equals that value." A second argument, the projection, chooses which fields come back. That's 90% of everyday MongoDB reading.

1Equality — the simplest filter

🗣 How to read a find() aloud db.customers.find({ vip: true, city: "Cairo" }) reads as: "In the customers collection, go through the documents one by one, and give me every document that looks like this example — vip is true AND city is Cairo." That's the mental model: the thing inside find(…) is not code, it's a mini example document to match against. Open 📂 Meet your data above and check by eye which customers should survive — then run it and confirm.

List every key you want to match. Multiple keys mean all must match (an implicit AND):

Match by one or more fields
both conditions must be true · try removing one

2Projection — choose what comes back

The second argument to find() is a projection. 1 = include, 0 = exclude. _id shows up unless you turn it off with _id: 0. Toggle the fields and watch the output shrink:

Field picker
flip the toggles and watch fields appear/disappear
Don't mix 1s and 0s (except _id) A projection is either an include list ({ name: 1, price: 1 }) or an exclude list ({ tags: 0 }). The only field you can always flip independently is _id.

3Matching values inside arrays

MongoDB is smart about arrays. { tags: 'milk' } matches any product whose tags array contains 'milk' — you don't write any special "contains" operator. Pick a tag and see:

"array contains" is automatic
no $contains needed — Mongo checks each element

4Nested fields & findOne

find() returns all matches; findOne() returns just the first (or null). Combine with a dotted path to reach into sub-documents:

find — all matches
findOne — just the first
The mental model First argument = which documents (the filter). Second argument = which fields (the projection). Everything else — sorting, counting, operators — builds on this pair.
🧠

Checkpoint — find()

1. db.products.find({ category: "Tea", price: 3.5 }) returns documents where…
Multiple fields in one filter document are ANDed together — all must match.
2. What does the projection { name: 1, _id: 0 } do?
Include name, and explicitly drop the default _id. The result documents have just one field.
3. { tags: "hot" } on a document whose tags is ["hot","milk"]
Equality against an array field means "contains this element." Very handy, and a common surprise for SQL folks.
🔓 You're reading a free chapter of MongoDB — the first two are open.
Unlock the rest of this course with a one-time payment.
Unlock this course →
🔓 See course prices