Error
Error Code:
263
MongoDB Error 263: Unsupported Operation in Transaction
Description
This error occurs when an operation that is not permitted within a multi-document transaction is attempted. MongoDB multi-document transactions have specific limitations on the types of commands and operations that can be executed within their scope, often related to schema modifications or certain administrative tasks.
Error Message
Operation Not Supported In Transaction
Known Causes
3 known causesAttempting DDL Operations
Executing schema modification commands, such as creating or dropping collections and indexes, is not allowed within multi-document transactions.
Executing Non-Transactional Commands
Certain administrative commands, like `createUser` or `shutdown`, cannot be run inside a transaction and will trigger this error.
Unsupported Read/Write Operations
Some specific read or write operations, particularly those that implicitly create new collections or databases, are disallowed within the context of a transaction.
Solutions
3 solutions available1. Move Operation Outside Transaction medium
Some operations can't run in transactions
1
Common unsupported operations in transactions
// These DON'T work in transactions:
db.createCollection("new") // DDL operations
db.collection.createIndex() // Index operations
db.collection.drop() // Collection operations
db.collection.aggregate([{$out: "other"}]) // $out/$merge stages
2
Run DDL before/after transaction
// Create collection first
db.createCollection("orders");
// Then use transaction for data operations
const session = client.startSession();
session.startTransaction();
await db.orders.insertOne({ item: "test" }, { session });
await session.commitTransaction();
2. Handle $out/$merge Differently medium
Output stages can't be in transactions
1
Use two-step approach
// Instead of $out in transaction:
const session = client.startSession();
session.startTransaction();
// Step 1: Get results into array (in transaction)
const results = await db.source.aggregate([
{ $match: { status: 'active' } },
{ $project: { name: 1 } }
], { session }).toArray();
// Step 2: Insert results (still in transaction)
await db.target.insertMany(results, { session });
await session.commitTransaction();
3. Use Read-Only Aggregations easy
Aggregations without write stages work
1
Read-only aggregation in transaction
const session = client.startSession();
session.startTransaction();
// This works (no $out/$merge):
const results = await db.orders.aggregate([
{ $match: { user_id: userId } },
{ $lookup: { from: 'products', ... } },
{ $group: { _id: '$status', total: { $sum: 1 } } }
], { session }).toArray();
await session.commitTransaction();