CRUD OperationsCore· 35 min read

Finding Documents

Read data back out — all of it, or just the documents that match a filter.

What you will learn

  • Find all and find one
  • Filter with a query object
  • Pick specific fields (projection)

find and findOne

find() returns all matching documents; findOne() returns the first match. The filter is just an object describing what to match.

find (many) and findOne (one)
db.users.find()                         // all users
db.users.find({ city: "Bengaluru" })    // only users in Bengaluru
db.users.findOne({ name: "Asha" })      // the first Asha

Line by line: find() with empty parentheses returns every document in the collection. Put a filter object inside — { city: "Bengaluru" } — and you get back only the documents whose city field equals that value. findOne(...) works the same way but stops at the first match and returns just that one document instead of a list.

Note: Output (from the findOne call): { _id: ObjectId('65f...a1'), name: 'Asha', age: 22, city: 'Bengaluru' } find() would instead give you an array of documents, while findOne() returns a single document object. If nothing matches, findOne returns null and find returns an empty list [ ].

Projection — choose fields

Pass a second object to choose which fields come back (1 = include, 0 = exclude).

Projection limits the fields returned
// Return only name and email, hide _id
db.users.find({}, { name: 1, email: 1, _id: 0 })

The first {} is the filter (empty, so it matches every user). The second object is the *projection* — it picks which fields you want back. name: 1 and email: 1 say “include these”; _id: 0 says “leave the id out”. This is handy when documents are large and you only need a couple of fields.

Note: Output: [ { name: 'Asha', email: 'asha@example.com' }, { name: 'Ravi', email: 'ravi@example.com' } ] Every field you did not ask for is gone — you get a tidy list with just name and email.

Reading is the R in CRUD. Whenever you fetch data, you follow the same few steps:

  1. Pick the method — find() for many documents, findOne() for just the first match.
  2. Write a filter object describing what to match (leave it {} to match everything).
  3. Optionally add a projection — a second object choosing which fields come back (1 include, 0 exclude).
  4. MongoDB returns the result — an array of documents from find, or a single document (or null) from findOne.

Tip: An empty filter { } matches everything. Add fields to narrow it: { city: "Bengaluru", age: 22 } matches documents where both are true.

Q. What does db.users.find({ age: 22 }) return?

Answer: find() returns all documents matching the filter — here, every user whose age field equals 22.

✍️ Practice

  1. Find all documents, then find only those matching a field.
  2. Use projection to return only two fields.

🏠 Homework

  1. Find all products that are in stock, returning only their names and prices.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →