MongoDB Find Documents
find() reads documents from a collection. A filter decides which documents, and a projection decides which fields.
Find All Documents
Example
db.students.find()An empty find() returns every document. With the _id and all fields, the output is long, so the examples below ask only for the fields they need.
Filter by Equality
The first argument is a filter document. This finds students from Karachi:
Example
db.students.find({ city: "Karachi" }, { _id: 0, name: 1, marks: 1 })[
{ name: 'Ali Khan', marks: 78 },
{ name: 'Bilal Ahmed', marks: 64 },
{ name: 'Hamza Raza', marks: 70 }
]Several fields in one filter must all match (like AND in SQL):
Example
db.students.find({ city: "Karachi", course: "Web Development" }, { _id: 0, name: 1 })[ { name: 'Ali Khan' }, { name: 'Bilal Ahmed' } ]Projection: Choose the Fields
The second argument is the projection:
1means "include this field",0means "leave it out"_idis always included unless you write_id: 0
findOne()
findOne() returns the first matching document itself, not a list:
Example
db.students.findOne({ name: "Fatima Noor" }, { _id: 0, name: 1, course: 1 }){ name: 'Fatima Noor', course: 'Graphic Designing' }It returns null if nothing matches.
Comparison Operators
Operators start with a $ sign and go inside the field's value:
| Operator | Meaning | Example |
|---|---|---|
$eq | Equal | { age: { $eq: 20 } } |
$ne | Not equal | { city: { $ne: "Karachi" } } |
$gt | Greater than | { marks: { $gt: 80 } } |
$gte | Greater than or equal | { marks: { $gte: 85 } } |
$lt | Less than | { age: { $lt: 20 } } |
$lte | Less than or equal | { age: { $lte: 19 } } |
$in | In a list | { city: { $in: ["Lahore", "Islamabad"] } } |
$nin | Not in a list | { city: { $nin: ["Karachi"] } } |
Students with more than 80 marks:
Example
db.students.find({ marks: { $gt: 80 } }, { _id: 0, name: 1, marks: 1 })[
{ name: 'Ayesha Siddiqui', marks: 85 },
{ name: 'Fatima Noor', marks: 92 },
{ name: 'Sana Iqbal', marks: 88 }
]A range, ages 19 to 21:
Example
db.students.find({ age: { $gte: 19, $lte: 21 } }, { _id: 0, name: 1, age: 1 })[
{ name: 'Ali Khan', age: 19 },
{ name: 'Ayesha Siddiqui', age: 21 },
{ name: 'Fatima Noor', age: 20 }
]Logical Operators: $or, $and, $not
$or takes an array of conditions; a document matches if any of them is true:
Example
db.students.find(
{ $or: [ { city: "Islamabad" }, { marks: { $lt: 65 } } ] },
{ _id: 0, name: 1, city: 1, marks: 1 }
)[
{ name: 'Bilal Ahmed', city: 'Karachi', marks: 64 },
{ name: 'Fatima Noor', city: 'Islamabad', marks: 92 }
]$and is needed only when you combine two conditions on the same operator, such as two $or groups. Usually listing fields side by side is enough.
Missing Fields: $exists
Find students who have no course field yet:
Example
db.students.find({ course: { $exists: false } }, { _id: 0, name: 1 })[ { name: 'Hamza Raza' } ]Querying Arrays
A plain value matches if the array contains it:
Example
db.students.find({ skills: "Python" }, { _id: 0, name: 1, skills: 1 })[
{ name: 'Ayesha Siddiqui', skills: [ 'Python' ] },
{ name: 'Sana Iqbal', skills: [ 'Python', 'SQL' ] }
]$all needs every listed value, and $size checks the array length: { skills: { $size: 2 } }.
Nested Fields
Use dot notation in quotes to reach inside an embedded document: { "contact.phone": "0300-1112233" }, or inside an array of documents: { "modules.week": 1 }.
Text Patterns
A regular expression searches inside text. /^A/ means "starts with A", and the i flag ignores case:
Example
db.students.find({ name: /^a/i }, { _id: 0, name: 1 })[ { name: 'Ali Khan' }, { name: 'Ayesha Siddiqui' } ]In mongosh, find() shows the first 20 documents and then prints Type "it" for more. Type it to see the next batch.
Exercise
Exercise
Which filter finds students whose marks are 70 or less?
