Read documents
Read documents
find returns a cursor over matching documents, while findOne returns one matching document or null. A filter selects records; a projection selects the fields returned.
Read operations allow you to retrieve data stored in MongoDB collections. The two most commonly used functions for this purpose are find() and findOne().
MongoDB queries are performed on collections using filter objects that specify the criteria for matching documents.
Before working through the examples, make sure you can connect with mongosh and select your database. Use a practice database for writes and deletions.
By the end of this lesson
Predict which documents a filter returns and choose between find and findOne.
find() vs findOne()
find()returns a cursor to all matching documents.findOne()returns the first matched document.
Step 1: Insert Sample Data
Let’s create a students collection with multiple documents.
db.students.insertMany([
{ name: "Alice", age: 22, grade: "A", subjects: ["Math", "Physics"] },
{ name: "Bob", age: 24, grade: "B", subjects: ["English", "History"] },
{ name: "Charlie", age: 22, grade: "A", subjects: ["Biology", "Chemistry"] },
{ name: "David", age: 23, grade: "C", subjects: ["Math", "English"] }
]);
{
acknowledged: true,
insertedIds: { ...ObjectIds... }
}
Step 2: Retrieve All Documents
Use find() with an empty filter {} to fetch all documents.
db.students.find({});
{ _id: ..., name: "Alice", age: 22, grade: "A", subjects: [ "Math", "Physics" ] }
{ _id: ..., name: "Bob", age: 24, grade: "B", subjects: [ "English", "History" ] }
{ _id: ..., name: "Charlie", age: 22, grade: "A", subjects: [ "Biology", "Chemistry" ] }
{ _id: ..., name: "David", age: 23, grade: "C", subjects: [ "Math", "English" ] }
This returns a cursor. In the MongoDB shell, results are printed directly. In code (Node.js or Python), you need to iterate over the cursor.
Step 3: Filtering Documents
Example data reference: the following setup creates the records used below. If you already inserted them earlier in this lesson, do not insert them again.
db.students.insertMany([
{ name: "Alice", age: 22, grade: "A", subjects: ["Math", "Physics"] },
{ name: "Bob", age: 24, grade: "B", subjects: ["English", "History"] },
{ name: "Charlie", age: 22, grade: "A", subjects: ["Biology", "Chemistry"] },
{ name: "David", age: 23, grade: "C", subjects: ["Math", "English"] }
]);Let’s filter documents where grade is "A".
db.students.find({ grade: "A" });
{ _id: ..., name: "Alice", age: 22, grade: "A", subjects: [ "Math", "Physics" ] }
{ _id: ..., name: "Charlie", age: 22, grade: "A", subjects: [ "Biology", "Chemistry" ] }
Q: How does MongoDB know which documents to return?
A: It compares each document's grade field to "A" and returns matches. If no document matches, it returns an empty result.
Step 4: Use findOne() to Get a Single Document
db.students.findOne({ age: 22 });
{ _id: ..., name: "Alice", age: 22, grade: "A", subjects: [ "Math", "Physics" ] }
Even though two students are aged 22, findOne() returns only the first match.
Step 5: Use Comparison Operators
Example data reference: the following setup creates the records used below. If you already inserted them earlier in this lesson, do not insert them again.
db.students.insertMany([
{ name: "Alice", age: 22, grade: "A", subjects: ["Math", "Physics"] },
{ name: "Bob", age: 24, grade: "B", subjects: ["English", "History"] },
{ name: "Charlie", age: 22, grade: "A", subjects: ["Biology", "Chemistry"] },
{ name: "David", age: 23, grade: "C", subjects: ["Math", "English"] }
]);Get students older than 22 using the $gt operator.
db.students.find({ age: { $gt: 22 } });
{ _id: ..., name: "Bob", age: 24, grade: "B", subjects: [ "English", "History" ] }
{ _id: ..., name: "David", age: 23, grade: "C", subjects: [ "Math", "English" ] }
Step 6: Projecting Specific Fields
You can limit the fields returned using projection. For example, get only name and grade:
db.students.find({ grade: "A" }, { name: 1, grade: 1, _id: 0 });
{ name: "Alice", grade: "A" }
{ name: "Charlie", grade: "A" }
We used _id: 0 to exclude the default _id field.
Step 7: Combining Multiple Conditions
Example data reference: the following setup creates the records used below. If you already inserted them earlier in this lesson, do not insert them again.
db.students.insertMany([
{ name: "Alice", age: 22, grade: "A", subjects: ["Math", "Physics"] },
{ name: "Bob", age: 24, grade: "B", subjects: ["English", "History"] },
{ name: "Charlie", age: 22, grade: "A", subjects: ["Biology", "Chemistry"] },
{ name: "David", age: 23, grade: "C", subjects: ["Math", "English"] }
]);Let’s find students who are aged 22 and have grade "A".
db.students.find({ age: 22, grade: "A" });
{ _id: ..., name: "Alice", age: 22, grade: "A", subjects: [ "Math", "Physics" ] }
{ _id: ..., name: "Charlie", age: 22, grade: "A", subjects: [ "Biology", "Chemistry" ] }
Step 8: Querying Arrays
Find students who have "Math" in their subjects.
db.students.find({ subjects: "Math" });
{ _id: ..., name: "Alice", ... }
{ _id: ..., name: "David", ... }
MongoDB automatically checks if "Math" exists in the array field subjects.
Check your understanding
What to remember
find returns a cursor over matching documents, while findOne returns one matching document or null. A filter selects records; a projection selects the fields returned.
Summary
find()retrieves all matching documentsfindOne()retrieves a single matching document- You can filter documents using field values and operators like
$gt,$lt,$in, etc. - Projections control which fields are returned
- Arrays can be queried just like regular fields
Mastering find() and findOne() is essential to efficiently retrieving and understanding your data in MongoDB.
Reaching the recap and completing the quizzes are separate milestones.