MongoDB Tutorial menu

MongoDB Find Documents

find() reads documents from a collection. A filter decides which documents, and a projection decides which fields.

Find All Documents

Example

javascript
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

javascript
db.students.find({ city: "Karachi" }, { _id: 0, name: 1, marks: 1 })
Output
[
  { 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

javascript
db.students.find({ city: "Karachi", course: "Web Development" }, { _id: 0, name: 1 })
Output
[ { name: 'Ali Khan' }, { name: 'Bilal Ahmed' } ]

Projection: Choose the Fields

The second argument is the projection:

  • 1 means "include this field", 0 means "leave it out"
  • _id is always included unless you write _id: 0

findOne()

findOne() returns the first matching document itself, not a list:

Example

javascript
db.students.findOne({ name: "Fatima Noor" }, { _id: 0, name: 1, course: 1 })
Output
{ 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:

OperatorMeaningExample
$eqEqual{ age: { $eq: 20 } }
$neNot equal{ city: { $ne: "Karachi" } }
$gtGreater than{ marks: { $gt: 80 } }
$gteGreater than or equal{ marks: { $gte: 85 } }
$ltLess than{ age: { $lt: 20 } }
$lteLess than or equal{ age: { $lte: 19 } }
$inIn a list{ city: { $in: ["Lahore", "Islamabad"] } }
$ninNot in a list{ city: { $nin: ["Karachi"] } }

Students with more than 80 marks:

Example

javascript
db.students.find({ marks: { $gt: 80 } }, { _id: 0, name: 1, marks: 1 })
Output
[
  { name: 'Ayesha Siddiqui', marks: 85 },
  { name: 'Fatima Noor', marks: 92 },
  { name: 'Sana Iqbal', marks: 88 }
]

A range, ages 19 to 21:

Example

javascript
db.students.find({ age: { $gte: 19, $lte: 21 } }, { _id: 0, name: 1, age: 1 })
Output
[
  { 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

javascript
db.students.find(
  { $or: [ { city: "Islamabad" }, { marks: { $lt: 65 } } ] },
  { _id: 0, name: 1, city: 1, marks: 1 }
)
Output
[
  { 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

javascript
db.students.find({ course: { $exists: false } }, { _id: 0, name: 1 })
Output
[ { name: 'Hamza Raza' } ]

Querying Arrays

A plain value matches if the array contains it:

Example

javascript
db.students.find({ skills: "Python" }, { _id: 0, name: 1, skills: 1 })
Output
[
  { 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

javascript
db.students.find({ name: /^a/i }, { _id: 0, name: 1 })
Output
[ { 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?

Learn MongoDB with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp