Assignments Of Day 6
MongoDB CRUD Operations – Part 2 (Find)
Assignment 1: Basic Querying
Question:
Write a query to retrieve all documents from the students collection.
Solution:
To retrieve all documents in the collection, simply call the find() method without any conditions.
db.students.find()
This query will return all documents in the students collection.
Assignment 2: Simple Filter Query
Question:
Write a query to retrieve all documents where the age of the student is 22.
Solution:
To filter documents based on the age field, pass a query object to the find() method:
db.students.find({ age: 22 })
This will return all documents where the age is exactly 22.
Assignment 3: Using Comparison Operators
Question:
Write a query to find all students whose age is greater than or equal to 18.
Solution:
We can use the $gte operator to find students whose age is greater than or equal to 18:
db.students.find({ age: { $gte: 18 } })
This will return all documents where the age is greater than or equal to 18.
Assignment 4: Using $in Operator
Question:
Write a query to find all students whose age is either 20, 22, or 24.
Solution:
The $in operator allows matching any of the specified values in an array.
db.students.find({ age: { $in: [20, 22, 24] } })
This will return all students whose age is one of 20, 22, or 24.
Assignment 5: Multiple Conditions with $and
Question:
Write a query to find all students whose age is greater than 18 and grade is greater than 75.
Solution:
By default, MongoDB uses $and when multiple conditions are given. Here's how you can do it:
db.students.find({ age: { $gt: 18 }, grade: { $gt: 75 } })
This will return all students who meet both conditions: age > 18 and grade > 75.
Assignment 6: Using $or Operator
Question:
Write a query to find all students who either have an age of 21 or a grade greater than or equal to 80.
Solution:
The $or operator allows you to find documents that satisfy at least one of the conditions:
db.students.find({
$or: [{ age: 21 }, { grade: { $gte: 80 } }]
})
This query returns all students who are either 21 years old or have a grade greater than or equal to 80.
Assignment 7: Querying Nested Fields
Question:
Write a query to find all students who live in the city of New York.
Solution:
You can query nested fields using dot notation. Here's how to do it:
db.students.find({ "address.city": "New York" })
This query will return all students who have New York as their city in the address sub-document.
Assignment 8: Limiting the Results
Question:
Write a query to return only the first 3 documents from the students collection.
Solution:
To limit the number of results, use the limit() method:
db.students.find().limit(3)
This will return only the first 3 documents from the collection.
Assignment 9: Sorting Query Results
Question:
Write a query to retrieve all students sorted by their age in descending order.
Solution:
Use the sort() method to sort the results. Pass { age: -1 } to sort in descending order.
db.students.find().sort({ age: -1 })
This will return all students sorted by age in descending order.
Assignment 10: Projecting Specific Fields
Question:
Write a query to retrieve only the name and age fields of all students, excluding the _id field.
Solution:
You can project specific fields by passing a second argument to find(). Use { _id: 0 } to exclude the _id field.
db.students.find({}, { name: 1, age: 1, _id: 0 })
This will return only the name and age fields from all documents, excluding the _id field.
Conclusion:
These assignments demonstrate different ways to query data in MongoDB using the find() method. You have learned how to:
- Perform basic queries
- Use operators like $gt, $gte, $in, $or, etc.
- Query nested fields
- Limit, sort, and project results
By completing these assignments, you will have gained a deeper understanding of how to work with MongoDB queries in a variety of scenarios.
