mongodb/node-mongodb-native · error · BSONError

Unsupported BSON type

Error message

Unsupported BSON type: ${as}

What it means

Thrown by the OnDemandDocument.toJSValue switch when it is asked to materialize a BSON type it does not handle (the default case). The driver's on-demand reader only supports the subset of BSON types needed for its own internals (null, numbers, bool, objectId, timestamp, string, binData, date, object, array). If driver code requests a type outside that set (e.g. regex, javascript, decimal128, dbPointer), it throws a BSONError. This is an internal-programming-error path, not a user-input path.

Solutions

  1. Upgrade the mongodb driver to the latest version.
  2. Upgrade mongod/mongos to a mutually compatible version.
  3. Report the issue to the driver team with the command and server version that triggered it.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await client.connect();
} catch (e) {
  if (e instanceof BSONError && /Unsupported BSON type/.test(e.message)) {
  }
}

Prevention

When it happens

Trigger: Fires at src/cmap/wire_protocol/on_demand/document.ts:230 only when toJSValue is called with an `as` BSON type constant that matches the element's type but is absent from the switch cases. Because the public API never exposes toJSValue (@internal class), a user seeing this means driver internals attempted to read an unsupported type from a server response.

Common situations: A driver version mismatch where new server response fields carry BSON types the on-demand reader cannot parse; a regression in driver internals; a prerelease server adding response fields of unsupported types. Extremely rare for end users.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/f1f1684eb9a33bb5. Report an issue: GitHub.

Appendix: 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 dce7939f86)