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

87
Views
Getting error in mongodb cast issue for valid object id

My code was working fine but after taking fresh code I am getting this error but as I can see my id is valid Object Id

Cast to ObjectId failed for value "5848e9ecaec0f31372816a26" at path "_id" for model "user"

how to fix this?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You have to cast the data to mongodb object id. If you are using mongoose, you can actually do these below.

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

[your_mongodb_model/collection].query({ _id: ObjectId(id) });

or simply, you can do these,

[your_mongodb_model].query({ _id: mongoose.Types.ObjectId(id) });
over 4 years ago · Santiago Trujillo Report

0

The reason for this error is documented in this mongoose issue. A workaround is to downgrade to mongoose version <= 4.7.2 or use Node version >=6.

over 4 years ago · Santiago Trujillo 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!