0
0
MongoDBquery~20 mins

Commit and abort behavior in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Transaction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after a successful commit in a MongoDB transaction?

Consider a MongoDB transaction that inserts a document into the users collection and then commits successfully. What will be the result of querying users after the commit?

MongoDB
session.startTransaction();
db.users.insertOne({name: 'Alice'}, {session});
session.commitTransaction();
db.users.find({name: 'Alice'}).toArray();
A[{_id: ObjectId(...), name: 'Alice'}]
Bnull
CTransactionError
D[]
Attempts:
2 left
💡 Hint

Think about what happens to data after a commit in a transaction.

query_result
intermediate
2:00remaining
What happens to data after aborting a MongoDB transaction?

A transaction inserts a document into the orders collection but then aborts. What will be the result of querying orders for that document?

MongoDB
session.startTransaction();
db.orders.insertOne({orderId: 123}, {session});
session.abortTransaction();
db.orders.find({orderId: 123}).toArray();
ATransactionAbortedError
B[{orderId: 123}]
C[]
Dnull
Attempts:
2 left
💡 Hint

Consider what aborting a transaction does to the changes made inside it.

📝 Syntax
advanced
2:00remaining
Which option correctly starts and commits a MongoDB transaction?

Identify the option that uses correct syntax to start a session, begin a transaction, insert a document, and commit the transaction.

A
const session = client.startSession();
db.collection('products').insertOne({item: 'book'});
session.commitTransaction();
session.endSession();
B
const session = client.startSession();
session.startTransaction();
db.collection('products').insertOne({item: 'book'}, {session});
session.commitTransaction();
session.endSession();
C
const session = client.startSession();
session.beginTransaction();
db.collection('products').insertOne({item: 'book'}, {session});
session.commit();
session.endSession();
D
const session = client.startSession();
session.startTransaction();
db.collection('products').insertOne({item: 'book'});
session.commitTransaction();
Attempts:
2 left
💡 Hint

Check the correct method names and usage of session in operations.

🔧 Debug
advanced
2:00remaining
Why does this MongoDB transaction fail to commit?

Given the code below, why does the transaction fail to commit?

MongoDB
const session = client.startSession();
session.startTransaction();
db.collection('inventory').updateOne({sku: 'abc'}, {$inc: {qty: -1}}, {session});
session.commitTransaction();
session.endSession();
AThe update operation is missing the session option, so it is not part of the transaction.
BThe transaction cannot update documents, only insert.
CcommitTransaction() must be called before startTransaction().
DendSession() must be called before commitTransaction().
Attempts:
2 left
💡 Hint

Think about how MongoDB knows which operations belong to the transaction.

🧠 Conceptual
expert
3:00remaining
What is the effect of a multi-document transaction abort in MongoDB?

In a multi-document transaction that modifies several collections, what happens if the transaction aborts?

AChanges are partially saved depending on the order of operations.
BOnly changes in the first collection are rolled back; others remain.
CThe transaction abort causes a partial commit of changes.
DAll changes made in all collections during the transaction are rolled back, leaving the database unchanged.
Attempts:
2 left
💡 Hint

Recall the atomicity property of transactions.