Skip to main content
MongoDB
CHAPTER 07 Beginner

MongoDB Find Queries | find(), findOne(), and Filtering

Updated: May 16, 2026
15 min read

# CHAPTER 7

Finding and Querying Documents

1. Introduction

Putting data into a database is easy. The true test of a database is how quickly and accurately you can get that data back out. This is the R in CRUD (Read). In SQL, you use the SELECT statement. In MongoDB, you use the incredibly powerful find() and findOne() methods. In this chapter, we will learn how to interrogate our collections, filtering massive datasets to return the exact documents our applications need.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Retrieve all documents in a collection using find().
  • Retrieve a single, specific document using findOne().
  • Filter queries by passing a query document (the MongoDB WHERE clause).
  • Shape the output by selectively returning fields using Projection.
  • Format console output using .pretty().

3. Fetching Everything (find())

To view every single document inside a collection, you call the find() method without passing any arguments into the parentheses.
javascript
12
// SQL Equivalent: SELECT * FROM employees;
db.employees.find()

*Tip: If the output looks like a giant, unreadable block of text, append .pretty() to format it nicely! Example: db.employees.find().pretty() (Note: Modern versions of mongosh do this automatically).*

4. Fetching a Single Document (findOne())

If you are building a "User Profile" page, you don't want a list of 10,000 users. You want exactly ONE user. The findOne() method searches the database, stops the millisecond it finds the very first matching document, and returns it as a clean JSON object.
javascript
12
// Just grab the very first employee in the collection
db.employees.findOne()

5. Filtering Data (The MongoDB WHERE Clause)

To find specific data, you pass a Query Document inside the find() parentheses. Think of this as filling out a search filter form.
javascript
12345678
// Find all employees who work in the HR department
db.employees.find({ department: "HR" })

// Find the specific employee named Alice
db.employees.findOne({ first_name: "Alice" })

// Find an employee by their exact unique ObjectId!
db.employees.findOne({ _id: ObjectId("650a2b9f8f1b2c3d4e5f6a7b") })

6. Multiple Filters (Implicit AND)

If you want to find someone who works in Sales AND has a salary of 60000, you simply put both keys in the query document. MongoDB automatically treats comma-separated keys as an AND condition.
javascript
1234
db.employees.find({ 
    department: "Sales", 
    salary: 60000 
})

7. Projection (Selecting Specific Fields)

If your employees collection has 50 fields (SSN, home address, medical history) and you are just building a public company directory, running find() will download massive amounts of sensitive, unnecessary data over the network. To fix this, we use Projection. It is the second argument passed to find().

In the projection document, a 1 means "Show this field", and a 0 means "Hide this field".

javascript
123456
// Give me ALL employees, but ONLY show their first_name and department.
// SQL Equivalent: SELECT first_name, department FROM employees;
db.employees.find(
    {}, // The Query (Empty means find all)
    { first_name: 1, department: 1 } // The Projection
)

*(Notice that MongoDB always returns the id field by default! If you want to hide the ID, you must explicitly tell it: { firstname: 1, _id: 0 }).*

8. Mini Project: The E-Commerce Catalog

Let's search an e-commerce database. We want to find a specific product, but we only want to see its name and price.
javascript
1234567891011
db.products.find(
    // 1. The Filter (WHERE category = 'Electronics')
    { category: "Electronics" }, 
    
    // 2. The Projection (SELECT name, price)
    { name: 1, price: 1, _id: 0 } 
)

// Output:
// { "name": "Laptop", "price": 999 }
// { "name": "Headphones", "price": 199 }

9. Common Mistakes

  • String vs ObjectId Queries: If you copy an ID string from a webpage and run db.users.find({ id: "650a2b..." }), it will return nothing! The id is stored as a special binary BSON type, not a string. You must wrap it: db.users.find({ id: ObjectId("650a2b...") }).
  • Mixing 1s and 0s in Projection: In a projection document, you cannot mix inclusion (1) and exclusion (0), except for the id field. You must either say "only show these 5 fields" OR "show everything except these 2 fields".

10. Best Practices

  • Never find() without Projection in Production: Just like SELECT * is a cardinal sin in SQL, executing a blind find() without a projection in Node.js or PHP will eat up massive amounts of server RAM and bandwidth. Always project only the fields your application screen actually needs to render.

11. Exercises

  1. 1. Write the command to find a single document where the username is "admin".
  1. 2. Write a find() query to fetch all documents where status is "active", but use projection to ONLY return the email field (and hide the _id).

12. MongoDB Challenges

Write a query to find all users located in the city of "London". (Assume the structure is: { name: "John", address: { city: "London" } }). *Hint: To query a nested document, wrap the dot-notation key in quotes!*
javascript
1
db.users.find({ "address.city": "London" })

13. MCQ Quiz with Answers

Question 1

What is the fundamental difference in output between find() and findOne()?

Question 2

In MongoDB, what is the purpose of "Projection" (the second argument in the find method)?

14. Interview Questions

  • Q: Compare and contrast the architecture of a SQL SELECT statement with a MongoDB find() method. How does MongoDB handle the SQL WHERE clause and column selection?
  • Q: Explain why a query attempting to find a document by its ID string (e.g., _id: "507f1...") will fail, and how to correct the query.

15. FAQs

Q: When I run db.users.find() on a massive collection, why does it only show the first 20 results? A: The MongoDB Shell automatically uses a "Cursor". It fetches the first 20 results so it doesn't crash your terminal. To see the next 20, you simply type it (Iterate) and hit Enter!

16. Summary

You now possess the power to interrogate the database. By mastering the query document to filter rows, and the projection document to shape the output, you can extract hyper-specific intelligence from millions of BSON documents in milliseconds.

17. Next Chapter Recommendation

We know how to insert data, and we know how to read it. But data changes. People move, prices increase, and passwords get reset. In Chapter 8: Updating Documents in MongoDB, we will master the U in CRUD, utilizing powerful Atomic Operators like $set and $inc.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·