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.
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 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
if (!ObjectId.isValid(id)) return res.status(400).end()- Trim and validate input
- Use the right id type for the collection
Detailed fix by platform
Node.js
const { ObjectId } = require("mongodb"); ObjectId.isValid("64b9f0c1e4b0a1b2c3d4e5f6") // true
How to diagnose
- Value — Valid 24 hex?
- Source — Client input?
- 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:
Similar errors
- Atlas: bad auth: Authentication failed MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password)
- BSONObj size is invalid / exceeds maximum document size (16MB) MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB)
- CastError ObjectId CastError: Cast to ObjectId failed for value "x" (type string) at path "_id"
Most viewed in MongoDB
- MongoParseError MongoDB: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
- bad auth : Authentication failed MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18)
- StrictPopulateError: Cannot populate path because it is not in your schema Mongoose 6+: StrictPopulateError: Cannot populate path `author` because it is not in your schema
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026