MONGODB Tutorial



MongoDB FIND


🔍 MongoDB Find Documents

The find() method is used to query documents in a MongoDB collection. It returns all documents that match a given filter.

🔸 Find All Documents

To fetch all documents from a collection, use an empty filter:

db.students.find({});
  

🔸 Find Documents Matching a Condition

Find students whose age is 21:

db.students.find({ age: 21 });
  

🔸 Find with Multiple Conditions

Find students named "Raj" and aged 22:

db.students.find({ name: "Raj", age: 22 });
  

🔸 Find with Comparison Operators

Find students older than 20:

db.students.find({ age: { $gt: 20 } });
  

🔸 Find and Project Specific Fields

Return only the name and age fields (exclude _id):

db.students.find(
  { age: { $gt: 20 } },
  { name: 1, age: 1, _id: 0 }
);
  

⚡ Quick Recap:

  • find({}) - Fetch all documents.
  • Use filter inside find() to specify criteria.
  • Use projection (2nd parameter) to specify which fields to return.
  • Comparison operators like $gt, $lt, $eq can be used.
Tip: You can chain .limit(), .sort(), and .skip() after find() to control your query results.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review