LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1216 min read

Finding Documents

Query a collection using find and findOne, and control result ordering, limits, and skipping.

find and findOne

find() returns a cursor over every matching document; findOne() returns just the first match (or null), useful when you expect at most one result.

db.users.find(); // every document in the collection
db.users.findOne({ email: "ada@example.com" }); // exactly one document, or null

Filtering with a Query Document

The first argument to find() is a query document — its fields describe conditions a matching document must satisfy.

db.users.find({ age: 28 }); // exact match
db.users.find({ "address.city": "London" }); // matching a nested field with dot notation

sort, limit, and skip

These chainable cursor methods control ordering and pagination.

db.users.find().sort({ age: -1 }); // descending by age
db.users.find().limit(10); // first 10 results
db.users.find().sort({ age: -1 }).skip(10).limit(10); // page 2 of 10
1 for Ascending, -1 for Descending

This convention (1/-1) appears throughout MongoDB's sort syntax and elsewhere — worth committing to memory early.

Counting Documents

db.users.countDocuments({ age: { $gte: 18 } }); // count matching a filter

Common Beginner Mistakes

Forgetting find() returns a cursor, not an array directly

In application code, you typically need to convert it (e.g. .toArray() in Node.js drivers) before using array methods on the results.

Using skip() for deep pagination on a large collection

skip() still scans and discards all preceding documents internally — for very large offsets, range-based pagination on an indexed field performs much better.

FAQs

Yes — an empty (or omitted) query document matches every document in the collection, just like find() with no arguments.

Yes — pass multiple keys in the sort document, e.g. { age: -1, name: 1 }, applied in the order they're listed.

Summary

find/findOne with sort/limit/skip cover the fundamentals of reading data back out of MongoDB. Next, you'll go deeper into the query operators that make filtering genuinely powerful.

Next Lesson →

Query Operators