0
0
MongoDBquery~20 mins

Bulk write operations in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bulk Write Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this bulkWrite operation?

Consider a MongoDB collection products with documents:

[{ _id: 1, name: "Pen", stock: 10 }, { _id: 2, name: "Pencil", stock: 5 }]

What will be the result of this bulkWrite operation?

db.products.bulkWrite([
  { updateOne: { filter: { _id: 1 }, update: { $inc: { stock: 5 } } } },
  { updateOne: { filter: { _id: 2 }, update: { $set: { stock: 0 } } } }
])
MongoDB
db.products.bulkWrite([
  { updateOne: { filter: { _id: 1 }, update: { $inc: { stock: 5 } } } },
  { updateOne: { filter: { _id: 2 }, update: { $set: { stock: 0 } } } }
])
A{"nMatched":2,"nModified":2,"ok":1}
B{"nMatched":1,"nModified":1,"ok":1}
C{"nMatched":2,"nModified":1,"ok":1}
D{"nMatched":1,"nModified":2,"ok":1}
Attempts:
2 left
💡 Hint

Each updateOne matches one document and modifies it.

📝 Syntax
intermediate
2:00remaining
Which bulkWrite syntax is valid to insert two new documents?

Choose the correct bulkWrite syntax to insert two new documents into a MongoDB collection.

Adb.collection.bulkWrite([{ insertOne: { doc: { name: "A" } } }, { insertOne: { doc: { name: "B" } } }])
Bdb.collection.bulkWrite([{ insertOne: { document: { name: "A" } } }, { insertOne: { document: { name: "B" } } }])
Cdb.collection.bulkWrite([{ insertOne: { document: { name: "A" } } }, { insertMany: { documents: [{ name: "B" }] } }])
Ddb.collection.bulkWrite([{ insert: { document: { name: "A" } } }, { insert: { document: { name: "B" } } }])
Attempts:
2 left
💡 Hint

The correct key for inserting a single document is insertOne with document field.

optimization
advanced
2:00remaining
How to optimize bulkWrite for mixed inserts and updates?

You want to insert 100 new documents and update 50 existing documents in one bulkWrite operation. Which approach is best for performance?

AUse one bulkWrite with 150 operations mixing insertOne and updateOne commands.
BRun 100 insertMany operations separately, then 50 updateOne operations separately.
CUse separate bulkWrite calls: one for all inserts, one for all updates.
DUse a single updateMany operation for all updates and insertOne for each insert.
Attempts:
2 left
💡 Hint

bulkWrite supports mixing different operation types in one call.

🔧 Debug
advanced
2:00remaining
Why does this bulkWrite operation fail with a syntax error?

Identify the syntax error in this bulkWrite command:

db.collection.bulkWrite([
  { updateOne: { filter: { _id: 1 }, update: { $set: { name: "X" } } } },
  { insertOne: { document: { name: "Y" } }
])
MongoDB
db.collection.bulkWrite([
  { updateOne: { filter: { _id: 1 }, update: { $set: { name: "X" } } } },
  { insertOne: { document: { name: "Y" } }
])
AInvalid use of $set operator in updateOne.
BMissing comma between updateOne and insertOne operations.
CUsing bulkWrite without a callback causes syntax error.
DMissing closing brace '}' for the insertOne operation object.
Attempts:
2 left
💡 Hint

Check the braces and commas carefully in the array of operations.

🧠 Conceptual
expert
2:00remaining
What happens if an unordered bulkWrite operation fails midway?

You run a bulkWrite with ordered: false containing 10 operations. The 5th operation fails due to a duplicate key error. What is the behavior of the remaining operations?

AOnly operations before the failure run; operations after are skipped.
BExecution stops immediately after the failure; remaining operations are not run.
CAll other operations continue to execute despite the failure.
DThe entire bulkWrite is rolled back; no changes are applied.
Attempts:
2 left
💡 Hint

Consider the difference between ordered and unordered bulkWrite behavior.