Finding Documents
10 min read
find and findOne
`find` returns a cursor over all matching documents. `findOne` returns the first match or null. Pass a filter object to narrow results — an empty filter {} matches everything.
js
// All students
db.students.find({});
// Students on the Full Stack course
db.students.find({ course: "Full Stack" });
// Single document
db.students.findOne({ name: "Aman Verma" });Iterating a Cursor
js
// In mongosh, use .toArray() or forEach
const results = await db.collection("students").find({ enrolled: true }).toArray();
results.forEach(student => {
console.log(student.name);
});In mongosh, find() automatically prints the first 20 results. Type it to iterate to the next batch.
Ready to test yourself?
Practice MongoDB— quiz & coding exercises