Error
Error Code:
65
MongoDB Error 65: Compound Operation Failure
Description
Error 65 indicates that a single database operation, or a set of operations executed together (e.g., a bulk write or transaction), encountered multiple distinct underlying errors. This error serves as a high-level summary, signaling that various components of a requested action failed simultaneously.
Error Message
Multiple Errors Occurred
Known Causes
4 known causesConcurrent Data Validation Issues
Multiple documents within a bulk insert or update operation failed to pass defined schema validations or data type checks.
Mixed Write Concern Violations
Different write operations within a batch failed to meet their specified write concern levels due to varying network, permission, or replica set availability issues.
Multiple Unique Constraint Breaches
Several documents in a bulk operation attempted to insert or update values that violated unique index constraints on different fields.
Underlying System Resource Exhaustion
Various operations failed due to a lack of available system resources such as memory, file handles, or disk space, affecting multiple concurrent writes.
Solutions
3 solutions available1. Inspect Individual Operation Errors easy
Examine the detailed error messages for each operation within the compound operation.
1
When MongoDB returns Error 65, it signifies that multiple operations within a single compound operation (like a transaction or a batched write) failed. You need to investigate the underlying reasons for each individual failure. The exact output will vary depending on how you are executing the compound operation, but typically, the error message will contain a list or array of individual error objects.
Example of how to interpret output from a `mongo` shell transaction:
javascript
try {
session.startTransaction();
db.collection1.insertOne({ _id: 1 });
db.collection2.updateOne({ _id: 2 }, { $set: { value: 'new' } }); // This might fail
db.collection3.insertOne({ _id: 3 });
session.commitTransaction();
} catch (e) {
console.error(e);
session.abortTransaction();
}
In the `catch` block, the `e` object will contain details about the transaction failure, often including a list of errors from the individual operations. Look for fields like `writeErrors` or similar structures that enumerate the problems.
2
For each individual error identified, determine the cause. Common causes include: constraint violations (e.g., duplicate keys), insufficient permissions, network issues, or document validation failures. Address each of these underlying issues.
If you see an error like `E11000 duplicate key error collection: mydatabase.mycollection index: _id_ dup key: { _id: 123 }`, it means you're trying to insert a document with an `_id` that already exists. The fix is to ensure your data adheres to unique key constraints.
2. Simplify and Isolate Failing Operations medium
Break down the compound operation into smaller, manageable parts to pinpoint the exact failing operation.
1
If you have a complex compound operation involving many steps, try executing them individually or in smaller batches. This will help you isolate which specific operation is causing the overall failure.
Consider a sequence of operations:
1. `db.collectionA.insertOne({...})`
2. `db.collectionB.updateOne({...})`
3. `db.collectionC.deleteOne({...})`
Execute these one by one in the `mongo` shell or your application code. If operation 2 fails, you know the problem lies there.
2
Once the problematic operation is identified, focus on resolving its specific error. This might involve correcting data, adjusting query logic, or ensuring correct schema/validation rules are in place.
If `db.collectionB.updateOne({...})` fails, examine the document it's trying to update and the update itself. Are there any document validation rules that are being violated by the update? Is the `_id` or other selector valid?
3. Review Transaction and Write Concern Settings medium
Ensure your transaction isolation levels and write concerns are appropriately configured and supported.
1
MongoDB transactions require specific configurations. Ensure your replica set is configured for journaling and that you are using a compatible MongoDB version (4.0+ for multi-document transactions).
Check your `mongod.conf` or `mongos.conf` for journaling enabled:
yaml
storage:
journal:
enabled: true
2
Verify that the `writeConcern` for your operations is set appropriately. For transactions, the default write concern is typically `majority`, which ensures durability. If you've overridden this, ensure it's compatible with transaction requirements.
When initiating a transaction, you can specify the `writeConcern` for the entire transaction. However, individual operations within a transaction will inherit the write concern from the session or use default settings if not explicitly overridden.
3
If you are using batched writes (e.g., `insertMany`, `updateMany` with `ordered: false`), the error message might be a composite of multiple individual write failures. If `ordered: true` (the default), the batch will stop on the first error.
Example of `ordered: false` for batched writes:
javascript
db.mycollection.insertMany([
{ a: 1 },
{ a: 1 }, // Duplicate key error
{ b: 2 }
], { ordered: false });
In this scenario, `insertMany` might return a compound error if multiple documents fail validation or violate unique constraints.