Consider a collection named students with the following document:
{
"_id" : ObjectId("5f0a7e80d8c9c7b5a48c49e1"),
"name" : "Alice",
"age" : 21,
"courses" : [
{
"name" : "Math",
"grade" : 89
},
{
"name" : "Science",
"grade" : 92
}
]
}
What is the query to update the grade of the "Math" course for the student with "_id" equal to ObjectId("5f0a7e80d8c9c7b5a48c49e1") to 95?
Answer: C
Question 2
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 3
Consider a MongoDB collection named products which has documents in the following format:
{
"_id" : ObjectId("5f5b0d2f3e3dfbcc11c84444"),
"name" : "Smartphone",
"brand" : "Apple",
"category" : "Electronics",
"price" : 1000,
"discount" : 0.1
}
What is the correct syntax to update the "price" field of all documents in the products collection by multiplying it with (1 - discount) using the $set operator and the $multiply operator?
Answer: D
Question 4
In a MongoDB database with referenced relationships, what is the advantage of using a separate collection for each entity over using a single collection for all entities?
Answer: A
Question 5
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?