mongodb/node-mongodb-native · error · BSONError

Unsupported BSON type: ${as}

Error message

Unsupported BSON type: ${as}

What it means

Thrown by the on-demand BSON decoder's toJSValue() when an element's type byte does not match any case the driver needs to function (the switch falls through to default). The decoder only revives the BSON types the driver itself uses internally; any other type byte yields 'Unsupported BSON type: <as>'. Surfaced as BSONError. Note this check is on the requested `as` type after the type-byte equality check, so it fires for type bytes outside the handled set.

Source

Thrown at src/cmap/wire_protocol/on_demand/document.ts:230

          );
        }

        return new Binary(
          this.bson.subarray(offset + 1 + 4, offset + 1 + 4 + totalBinarySize),
          subType
        );
      }
      case BSONType.date:
        // Pretend this is correct.
        return new Date(Number(NumberUtils.getBigInt64LE(this.bson, offset)));

      case BSONType.object:
        return new OnDemandDocument(this.bson, offset);
      case BSONType.array:
        return new OnDemandDocument(this.bson, offset, true);

      default:
        throw new BSONError(`Unsupported BSON type: ${as}`);
    }
  }

  /**
   * Returns the number of elements in this BSON document
   */
  public size() {
    return this.elements.length;
  }

  /**
   * Checks for the existence of an element by name.
   *
   * @remarks
   * Uses `getElement` with the expectation that will populate caches such that a `has` call
   * followed by a `getElement` call will not repeat the cost paid by the first look up.
   *
   * @param name - element name

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Upgrade the driver/bson to a version that supports the BSON type encountered.
  2. If decoding a known field, ensure the data is valid by re-reading from the server.
  3. Report the type byte value and field context to the driver/bson maintainers if it reproduces on the latest version.
Defensive patterns

Strategy: try-catch

Try / catch

import { BSONError } from 'bson';
try {
  await collection.findOne({ _id });
} catch (err) {
  if (err instanceof BSONError && /Unsupported BSON type/.test(err.message)) {
    // upgrade driver/bson; or isolate the document/field
  }
  throw err;
}

Prevention

When it happens

Trigger: A server response document contains a BSON element whose type byte is not in {null, undefined, double, int, long, bool, objectId, timestamp, string, binData, date, object, array}. Encountered when the driver lazily decodes a cursor/response field of an exotic or newer BSON type it does not revive on-demand. Corruption that produced a garbage type byte can also trigger it.

Common situations: A server newer than the driver emits a BSON type the driver version doesn't decode on-demand. Corrupt responses where a type byte is random. Decoding internal fields not expected to contain exotic types.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/f1f1684eb9a33bb5.json. Report an issue: GitHub.