The find() method is used to query documents in a MongoDB collection. It returns all documents that match a given filter.
To fetch all documents from a collection, use an empty filter:
db.students.find({});
Find students whose age is 21:
db.students.find({ age: 21 });
Find students named "Raj" and aged 22:
db.students.find({ name: "Raj", age: 22 });
Find students older than 20:
db.students.find({ age: { $gt: 20 } });
Return only the name and age fields (exclude _id):
db.students.find(
{ age: { $gt: 20 } },
{ name: 1, age: 1, _id: 0 }
);
find({}) - Fetch all documents.find() to specify criteria.$gt, $lt, $eq can be used..limit(), .sort(), and .skip() after find() to control your query results.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!