BSONError: input must be a 24 character hex string MongoDB Origin: Data OSI layer 7 · Application Last updated 5 Nov 2026

BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters

new ObjectId(value) received a value that isn't a valid 24-hex id.

Exact message you may see BSONError: input must be a 24 character hex string, 12 byte Uint8Array, or an integerBSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex charactersBSONTypeError: Argument passed in must be a single String of 12 bytes or a string of 24 hex charactersinput must be a 24 character hex string
Seen on: MongoDB Node.js

Meaning

IDs from URLs/forms may be undefined, "undefined", or numeric. Validate with ObjectId.isValid(id) before constructing and return 400/404 for invalid ones. Mongoose's equivalent is CastError for _id.

Common causes

  • Undefined/null id from the client
  • Id with whitespace or wrong length
  • Using numeric/UUID ids with ObjectId
  • Mixed up id formats between systems
  • Route param typo

⚡ Quick fix

  1. if (!ObjectId.isValid(id)) return res.status(400).end()
  2. Trim and validate input
  3. Use the right id type for the collection

Detailed fix by platform

Node.js

  1. const { ObjectId } = require("mongodb"); ObjectId.isValid("64b9f0c1e4b0a1b2c3d4e5f6") // true

How to diagnose

  1. Value — Valid 24 hex?
  2. Source — Client input?
  3. Type — ObjectId vs string?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

Atlas: bad auth: Authentication failed — MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password) (MongoDB)Atlas: bad auth: Auth…BSONObj size is invalid / exceeds maximum document size (16MB) — MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB) (MongoDB)BSONObj size is inval…CastError ObjectId — CastError: Cast to ObjectId failed for value "x" (type string) at path "_id" (Node.js)CastError ObjectIdCastError: Cast to Date/Number/Boolean failed — Mongoose: CastError: Cast to date failed for value "abc" (type string) at path "createdAt" (MongoDB)CastError: Cast to Da…Document failed validation — MongoDB: MongoServerError: Document failed validation (code 121) (MongoDB)Document failed valid…MongoDB Atlas: connection timed out — IP not whitelisted — MongoServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted (MongoDB)MongoDB Atlas: connec…Sort/Group exceeded memory limit of 100 MB (allowDiskUse) — MongoServerError: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in (MongoDB)Sort/Group exceeded m…BSONError…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.