⬅ Previous Topic
Insert Operations in MongoDBNext Topic ⮕
Query Filtering with Operators⬅ Previous Topic
Insert Operations in MongoDBNext Topic ⮕
Query Filtering with OperatorsRead operations allow you to retrieve data stored in MongoDB collections. The two most commonly used functions for this purpose are find()
and findOne()
.
MongoDB queries are performed on collections
using filter objects that specify the criteria for matching documents.
find()
returns a cursor to all matching documents.findOne()
returns the first matched document.Let’s create a students
collection with multiple documents.
db.students.insertMany([
{ name: "Alice", age: 22, grade: "A", subjects: ["Math", "Physics"] },
{ name: "Bob", age: 24, grade: "B", subjects: ["English", "History"] },
{ name: "Charlie", age: 22, grade: "A", subjects: ["Biology", "Chemistry"] },
{ name: "David", age: 23, grade: "C", subjects: ["Math", "English"] }
]);
Output:
{ acknowledged: true, insertedIds: { ...ObjectIds... } }
Use find()
with an empty filter {}
to fetch all documents.
db.students.find({});
Output:
{ _id: ..., name: "Alice", age: 22, grade: "A", subjects: [ "Math", "Physics" ] } { _id: ..., name: "Bob", age: 24, grade: "B", subjects: [ "English", "History" ] } { _id: ..., name: "Charlie", age: 22, grade: "A", subjects: [ "Biology", "Chemistry" ] } { _id: ..., name: "David", age: 23, grade: "C", subjects: [ "Math", "English" ] }
This returns a cursor. In the MongoDB shell, results are printed directly. In code (Node.js or Python), you need to iterate over the cursor.
Let’s filter documents where grade
is "A"
.
db.students.find({ grade: "A" });
Output:
{ _id: ..., name: "Alice", age: 22, grade: "A", subjects: [ "Math", "Physics" ] } { _id: ..., name: "Charlie", age: 22, grade: "A", subjects: [ "Biology", "Chemistry" ] }
Q: How does MongoDB know which documents to return?
A: It compares each document's grade
field to "A" and returns matches. If no document matches, it returns an empty result.
db.students.findOne({ age: 22 });
Output:
{ _id: ..., name: "Alice", age: 22, grade: "A", subjects: [ "Math", "Physics" ] }
Even though two students are aged 22, findOne()
returns only the first match.
Get students older than 22 using the $gt
operator.
db.students.find({ age: { $gt: 22 } });
Output:
{ _id: ..., name: "Bob", age: 24, grade: "B", subjects: [ "English", "History" ] } { _id: ..., name: "David", age: 23, grade: "C", subjects: [ "Math", "English" ] }
You can limit the fields returned using projection. For example, get only name
and grade
:
db.students.find({ grade: "A" }, { name: 1, grade: 1, _id: 0 });
Output:
{ name: "Alice", grade: "A" } { name: "Charlie", grade: "A" }
We used _id: 0
to exclude the default _id
field.
Let’s find students who are aged 22 and have grade "A".
db.students.find({ age: 22, grade: "A" });
Output:
{ _id: ..., name: "Alice", age: 22, grade: "A", subjects: [ "Math", "Physics" ] } { _id: ..., name: "Charlie", age: 22, grade: "A", subjects: [ "Biology", "Chemistry" ] }
Find students who have "Math" in their subjects.
db.students.find({ subjects: "Math" });
Output:
{ _id: ..., name: "Alice", ... } { _id: ..., name: "David", ... }
MongoDB automatically checks if "Math" exists in the array field subjects
.
find()
retrieves all matching documentsfindOne()
retrieves a single matching document$gt
, $lt
, $in
, etc.Mastering find()
and findOne()
is essential to efficiently retrieving and understanding your data in MongoDB.
⬅ Previous Topic
Insert Operations in MongoDBNext Topic ⮕
Query Filtering with OperatorsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.