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 2
When sharding a MongoDB collection, what is the main factor that determines the distribution of data across shards?
Answer: D
Question 3
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 4
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 5
You are working on a MongoDB database for a library system. The system tracks book information in a Books collection. A typical document in this collection looks like this:
Original Document:
{
"_id": ObjectId("book_id"),
"title": "The Art of Computer Programming",
"author": "Donald Knuth",
"genres": ["Computer Science", "Algorithms"],
"published_year": 1968,
"copies_available": 3
}
You are asked to update the document by replacing it with the following document:
Updated Document:
{
"title": "The Art of Computer Programming",
"published_year": 1973
}
After this update, what will the final document in the database look like?