mongodb/node-mongodb-native · error · MongoTransactionError
Transactions do not support unacknowledged write concern
Error message
Transactions do not support unacknowledged write concern
What it means
Thrown by the Transaction constructor when the resolved write concern has w === 0 (unacknowledged). MongoDB transactions require that every operation be acknowledged so the server can report commit/abort status; an unacknowledged write concern makes transaction semantics impossible. The driver rejects this at transaction creation time via MongoTransactionError.
Solutions
- Remove w:0 from the transaction options or use an acknowledged write concern (w:1, w:'majority', or omit it).
- If the client-level writeConcern is w:0, pass an explicit acknowledged write concern to startTransaction/withTransaction.
- Do not use unacknowledged writes for any operation that participates in a transaction.
Example fix
// before
session.startTransaction({ writeConcern: { w: 0 } });
// after
session.startTransaction({ writeConcern: { w: 'majority' } }); Defensive patterns
Strategy: validation
Validate before calling
function isAcknowledgedWC(wc: unknown): boolean {
if (!wc || typeof wc !== 'object') return true; // default is acknowledged
const w = (wc as any).w;
return w !== 0 && w !== '0';
}
if (!isAcknowledgedWC(txnOptions.writeConcern)) {
throw new Error('Refusing to start transaction with unacknowledged write concern');
} Type guard
function hasAcknowledgedWriteConcern(opts: { writeConcern?: any }): boolean {
const w = opts.writeConcern?.w;
return w === undefined || w === 1 || w === 'majority' || (typeof w === 'number' && w > 0);
} Try / catch
try {
session.startTransaction({ writeConcern: { w: 'majority' } });
} catch (e) {
if (e instanceof MongoTransactionError && /unacknowledged/.test(e.message)) {
session.startTransaction(); // retry with default acknowledged write concern
} else throw e;
} Prevention
- Never set w:0 on clients/sessions used for transactions.
- Keep a separate MongoClient with w:0 for fire-and-forget inserts, distinct from transactional clients.
- Validate writeConcern options in a shared helper before starting transactions.
When it happens
Trigger: Calling session.withTransaction(callback, { writeConcern: { w: 0 } }) or session.startTransaction({ writeConcern: { w: 0 } }). Also when the MongoClient-level writeConcern is set to w:0 and a transaction is started without overriding it.
Common situations: Reusing a fire-and-forget write concern (w:0) from high-throughput insert paths when running transactional code. Copy-pasting client options across contexts where transactional guarantees are needed.
Related errors
- An operation cannot be given a timeoutMS setting when…
- All values of tls/ssl must be the same.
- Argument "docs" must be an array of documents
- Argument "filter" must be an object
- Argument "operations" must be an array of documents
AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11).
Data as JSON: /api/errors/361822ad2ec3f1e9.
Report an issue: GitHub.
Appendix: source
Thrown at src/transactions.ts:93
/**
* @internal
*/
export class Transaction {
state: TxnState;
options: TransactionOptions;
_pinnedServer?: Server;
_recoveryToken?: Document;
/** Create a transaction */
constructor(options?: TransactionOptions) {
options = options ?? {};
this.state = TxnState.NO_TRANSACTION;
this.options = {};
const writeConcern = WriteConcern.fromOptions(options);
if (writeConcern) {
if (writeConcern.w === 0) {
throw new MongoTransactionError('Transactions do not support unacknowledged write concern');
}
this.options.writeConcern = writeConcern;
}
if (options.readConcern) {
this.options.readConcern = ReadConcern.fromOptions(options);
}
if (options.readPreference) {
this.options.readPreference = ReadPreference.fromOptions(options);
}
if (options.maxCommitTimeMS) {
this.options.maxTimeMS = options.maxCommitTimeMS;
}
// TODO: This isn't technically necessaryView on GitHub (pinned to dce7939f86)