You have a MongoDB collection named employees that contains documents with fields name, department, and salary. You want to update the salary of all employees in the "Marketing" department to $75,000. The original collection looks like this:
What will the collection look like after this update operation?
Answer: C
Question 2
How can we present the basic syntax for aggregation in MongoDB?
Answer: D
Question 3
What is the best practice in using the $match operator?
Answer: D
Question 4
You have two collections in your MongoDB database: students and grades. The students collection contains the following document:
{
"_id": ObjectId("64d5b0e5c9d95a76ac3ebed2"),
"student_id": 1,
"name": "Alice"
}
The grades collection contains the following documents:
{
"_id": ObjectId("64d5b0e5c9d95a76ac3ebed3"),
"student_id": 2,
"course": "Math",
"grade": "A"
},
{
"_id": ObjectId("64d5b0e5c9d95a76ac3ebed4"),
"student_id": 2,
"course": "Science",
"grade": "B"
}
You run the following aggregation query to retrieve students along with their grades:
db.students.aggregate([
{
$lookup: {
from: "grades",
localField: "student_id",
foreignField: "student_id",
as: "grades_details"
}
}
])
What will be the structure of the documents returned by this aggregation query for the student with student_id: 1?
Answer: B
Question 5
Consider the following documents in a MongoDB collection orders:
{
"_id": 1,
"customer_id": 101,
"item": "apple",
"price": 0.5,
"quantity": 100,
"date": ISODate("2022-01-01T00:00:00Z")
},
{
"_id": 2,
"customer_id": 102,
"item": "banana",
"price": 0.25,
"quantity": 50,
"date": ISODate("2022-01-02T00:00:00Z")
},
{
"_id": 3,
"customer_id": 101,
"item": "orange",
"price": 0.75,
"quantity": 75,
"date": ISODate("2022-01-03T00:00:00Z")
}
What is the aggregation pipeline to group the documents by "customer_id" and calculate the sum of the "quantity" field for each group, sorted in descending order by the sum of the "quantity" field?