Labs ICT
⭐ Pro Login

Find Documents

Querying and reading your data.

Finding Documents in MongoDB

Alright, you've got data in your database. Now let's talk about getting it back out. Finding documents in MongoDB is like asking a question to your data. "Hey MongoDB, show me all users named Alice" or "Give me everyone over 30." Simple queries, powerful results.

The two main methods here are find() and findOne(). find() returns a cursor to all matching documents, while findOne() returns just the first match. If you're expecting a single result, findOne() is your friendβ€”it's slightly more efficient and returns the document directly instead of a cursor.

One thing that trips up newcomers: find() doesn't actually execute the query until you iterate over the cursor. This is called lazy evaluation, and it's actually pretty smart. MongoDB waits until you actually need the data before doing the work.

Find All Documents

The simplest find() call with no arguments returns every document in the collection. It's like opening a book and reading every page. In production, you almost never want to do this on a large collection, but for small datasets or development, it's perfectly fine.

The cursor returned by find() is iterable. You can convert it to an array, loop through it, or chain methods like sort(), limit(), and skip() to refine your results. MongoDB gives you all the tools you need to slice and dice your data.

db.users.find()

db.users.find().toArray()

db.users.find().sort({ name: 1 }).limit(5)
Try it Yourself β†’

Querying with Conditions

This is where things get interesting. You can pass a query document to find() to filter results. It's like telling MongoDB exactly what you're looking for. You can match on exact values, use comparison operators, and combine multiple conditions.

The query syntax uses JavaScript objects, which should feel natural if you're coming from a JavaScript background. You're basically saying "find documents where this field equals this value" or "where this field is greater than this number." It reads almost like English once you get the hang of it.

db.users.find({ name: "Alice Johnson" })

db.users.find({ age: { $gte: 30 } })

db.users.find({ $and: [{ age: { $gte: 25 } }, { age: { $lte: 35 } }] })
Try it Yourself β†’

Projection: Selecting Fields

Projections let you control which fields MongoDB returns. Instead of getting the entire document every time, you can say "just give me the name and email." This saves bandwidth, reduces memory usage, and makes your queries faster. It's like asking for a summary instead of the full report.

The second argument to find() is the projection object. Set a field to 1 to include it, or 0 to exclude it. You can't mix inclusion and exclusion (except for the _id field, which you can exclude with 0). This keeps things clean and predictable.

db.users.find({}, { name: 1, email: 1 })

db.users.find({ age: { $gte: 30 } }, { name: 1, _id: 0 })
Try it Yourself β†’

πŸ§ͺ Quick Quiz

What does findOne() return?