Consider a MongoDB database containing a collection of documents representing online orders for an e-commerce website. The documents have the following structure:
Select an aggregation pipeline that returns the total sales amount by state and month, for orders placed in the year 2022. The result should have the following format:
Answer: A
Question 2
Consider a tasks collection that holds task records. The document structure is as follows:
{
"_id": ObjectId("64b64c58ed01c0a5e72dbf5f"),
"task": "Review PR",
"status": "pending",
"assignedTo": "alice"
}
Two operations are performed concurrently:
Operation A:
db.tasks.findAndModify({
query: { task: "Review PR", status: "pending" },
remove: true
});
Operation B:
db.tasks.findAndModify({
query: { task: "Review PR", status: "pending" },
update: { $set: { status: "completed" } },
new: true
});
Operation B starts slightly after Operation A but before Operation A completes. What will be the final state of the tasks collection after both operations have been executed?
Answer: A
Question 3
Which of the following command skips the first 10 documents in the movies collection and returns all remaining documents?
Answer: D
Question 4
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 5
Consider a MongoDB database managing e-commerce product data. The database has a collection named products with documents containing the fields product_id, category, price, and reviews. Each reviews field is an array of subdocuments with fields user_id, rating, and comment. You are tasked with optimizing a query that retrieves all products within a specific category that have an average rating higher than 4. The query is frequently used, and performance is critical. Which indexing strategy would best optimize this query?