You manage a MongoDB collection called inventory that stores documents for a store's inventory. Each document has the fields item, category, and quantity. You want to ensure that an item is always present in the collection, even if it needs to be inserted. Specifically, if the item "notebook" in the "Stationery" category does not exist, it should be inserted with a quantity of 50. If it does exist, its quantity should be updated to 50. You plan to use the following command:
db.inventory.updateOne(
{ "item": "notebook", "category": "Stationery" },
{ $set: { "quantity": 50 } },
{ upsert: true }
)
What will happen after executing this command if there is no "notebook" item in the "Stationery" category in the inventory collection?
Answer: D
Question 2
In MongoDB, which statement accurately describes a typical task for a MongoDB developer related to schema validation?
Answer: C
Question 3
Consider a MongoDB database containing a collection of documents representing product information for an e-commerce website. The documents have the following structure:
{ "username": "user3", "rating": 3, "comment": "Good but overpriced." }
]
}
Select the MongoDB aggregation pipeline that returns the average rating of all products grouped by brand. The result should include only brands with an average rating greater than or equal to 4. The output should have the following format:
Answer: D
Question 4
In your database there is a collection named trips with the following document structure:
How can you extract all trips from this collection ended at stations that are to the west of the -73.5 longitude coordinate?
Answer: C
Question 5
You are developing a MongoDB application where write performance is a critical requirement. Given a scenario where your application primarily performs insert operations and rarely performs updates or deletes, which of the following MongoDB features would best optimize this heavy-write workload?