The command has failed
This is a generic error wrapper used by older drivers and shells. It indicates that a command failed, but the actual, more specific error code and message are nested inside the error response object.
- 1This is a wrapper error. The true cause is in the nested error document.
- 2An older MongoDB driver or `mongo` shell might report this instead of the specific error code.
- 3Any command failure could be wrapped in this error.
- 4For example, a `DuplicateKey` error (11000) might be presented as a `CommandFailed` error (125).
This is a presentation-layer error. Any command can trigger it. For example, a duplicate key insert might be reported this way by an older client.
// A command that causes a more specific error.
db.users.createIndex({ email: 1 }, { unique: true });
db.users.insertOne({ email: "test@example.com" });
db.users.insertOne({ email: "test@example.com" });expected output
// An older client might show: CommandFailed: E11000 duplicate key error ...
Fix 1
Inspect the Nested Error Object
WHEN You receive this error code.
Why this works
Look at the full error response from the driver. It will contain a `code` or `errInfo` field with the true, specific error code (like 11000) and a more detailed message. This is the real error you need to debug.
Fix 2
Update Your MongoDB Driver
WHEN You consistently see this generic error instead of specific ones.
// For Node.js npm update mongodb
Why this works
Modern MongoDB drivers are better at unwrapping this error and presenting the specific underlying cause directly. Updating your driver can improve the debugging experience.
✕ Write application logic that only checks for code 125
This is too generic. Your error handling logic should be based on the specific, nested error code (e.g., 11000 for duplicates, 112 for write conflicts) to react appropriately.
mongodb/mongo src/mongo/base/error_codes.yml
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev