20
MongoDBERRORWrite ErrorHIGH confidence

An attempted operation is not supported

What this means

This error is thrown when an operation is logically invalid or not permitted on a certain type of object. For example, trying to apply an update operator to a system collection or attempting a conflicting modification in a single command.

Why it happens
  1. 1Attempting to rename the `_id` field in a document
  2. 2Trying to use `$rename` to move a field to be a sub-field of itself
  3. 3Using a write operator like `$set` on a system collection (e.g., `system.profile`)
  4. 4Performing a logically impossible update, such as incrementing a non-numeric field
How to reproduce

An update operation attempts to rename the immutable `_id` field.

trigger — this will error
trigger — this will error
db.c.insertOne({_id: 1, a: 1})
db.c.updateOne({_id: 1}, {$rename: {"_id": "id"}});

expected output

MongoServerError: The _id field cannot be changed.

Fix 1

Restructure the Update Operation

WHEN You are trying to modify an immutable or protected field.

Restructure the Update Operation
// Instead of renaming _id, create a new field with the same value.
db.c.updateOne({_id: 1}, {$set: {"new_id_field": 1}})

Why this works

Revise the operation to work within MongoDB's constraints. Immutable fields like `_id` cannot be changed; work around this by adding new fields rather than modifying the original.

Fix 2

Correct the Operator Usage

WHEN An operator is being used on an incorrect data type.

Correct the Operator Usage
// Ensure the target field is a number before using $inc
db.c.updateOne({_id: 1, a: "hello"}, {$set: {"a": 1}});
db.c.updateOne({_id: 1}, {$inc: {"a": 5}});

Why this works

Make sure that the operator you are using is compatible with the data type of the target field. For example, `$inc` only works on numeric fields.

What not to do

✕ Attempt to directly modify system collections

System collections are managed internally by MongoDB. Direct modification can lead to database corruption, instability, and unpredictable behavior.

Sources
Official documentation ↗

mongodb/mongo src/mongo/base/error_codes.yml

Update Operators ↗

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MongoDB errors