Complete the code to update a single field atomically in a MongoDB document.
db.collection.updateOne({ _id: 1 }, { $set: { status: [1] } })In MongoDB, string values must be in quotes. So to set the field status to the string "active", you need quotes.
Complete the code to start a transaction session in MongoDB.
const session = client.startSession();
session.[1]();beginTransaction() which is not a MongoDB method.The correct method to start a transaction in a MongoDB session is startTransaction().
Fix the error in the transaction commit code.
await session.[1]();commit() which does not exist in MongoDB sessions.commitTxn().The correct method to commit a transaction in MongoDB is commitTransaction().
Fill both blanks to update two fields atomically inside a transaction.
await collection.updateOne({ _id: 1 }, { $set: { status: [1], count: [2] } }, { session });Inside a transaction, you can update multiple fields atomically. Here, status is set to the string "completed" and count to the number 42.
Fill all three blanks to correctly start a transaction, update a document, and commit the transaction.
const session = client.startSession(); try { session.[1](); await collection.updateOne({ _id: 2 }, { $set: { processed: [2] } }, { session }); await session.[3](); } finally { await session.endSession(); }
false instead of true for the processed field.This code starts a transaction with startTransaction(), updates the processed field to true, and commits the transaction with commitTransaction().