Consider a MongoDB collection named orders which has documents in the following format:
{
"_id" : ObjectId("5f5b0d2f3e3dfbcc11c84444"),
"orderId" : 1001,
"customerId" : 200,
"items" : [
{
"productId" : 101,
"quantity" : 2
},
{
"productId" : 102,
"quantity" : 1
}
],
"totalAmount" : 250,
"status" : "Pending"
}
What is the correct syntax to find and update the first document in the orders collection where "status" is "Pending", and change the "status" field to "Completed" using the findAndModify method?
Answer: B
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
What is the correct syntax to insert multiple documents into a MongoDB collection named orders and return the _id values for all inserted documents?
Answer: B
Question 4
In a MongoDB collection named orders, you are tasked with retrieving a subset of documents that fulfill specific criteria. Each document in this collection contains the following fields: customerId, orderId, orderDate, and orderDetails (an embedded document with fields: product, quantity, unitPrice). You are required to fetch all orders made by the customer with a customerId of 1234 on a specific date 2023-04-12, and only return the orderId, orderDate, and product from orderDetails. Which of the following MongoDB queries would accurately fulfill these requirements?
Answer: A
Question 5
As a MongoDB Developer, you are tasked with optimizing a MongoDB database. Your collection has documents with the following structure:
{
"_id": ObjectId(),
"user_id": Number,
"user_data": {
"name": String,
"email": String,
"phone": String,
"address": {
"city": String,
"state": String,
"country": String
}
},
"transactions": [
{
"transaction_id": Number,
"amount": Number,
"date": Date
}
]
}
The application frequently queries for the total transaction amount for users based on the city. Which index should you create to optimize the performance of these queries?