Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
TypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters

Below is the code, ive also tried the $regex method, it only gives me the 'Cant use $regex' error.

let query : any;
        if (data.search.length > 0) {
            query = { _id: new RegExp(`^${data.search}.*`), accountId };
        } else {
            query = { accountId };
        }

        const orders: any = await TEMPOrders.find(query)

            .lean()
            .exec();
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can simply use ObjectId() for mongoose _id field for querying and the search value should also be an ObjectId,otherwise it will throw an error of string must be of length 12 bytes or 24 hex characters.

As _id is the primary key and it defines each documents uniquely in the collection. So ObjectId is used for comparison or for querying _id.

Import ObjectId as shown below:

var ObjectId = require('mongodb').ObjectId 

query = { _id: ObjectId(data.search), accountId };
about 4 years ago · Juan Pablo Isaza Report

0

To avoid this error must convert to objectID and also add term()

var ObjectId = require(mongodb).ObjectId

var id = (req.body.id).term();
db.col.findOne({'_id':ObjectId(id)},(err,doc)=>{
 if(err){
   console.log(err)
 }else{
   console.log(doc)
 }
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!