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

456
Views
values passed to the '_id' (mongodb) is not a valid ObjectId Pymongo

I have a python script that receive (from a node.js script) an _id for a mongodb document as an argument. Using that value I'm trying to query the db and retrieve a document.

However, when i try to run the script it throws an error saying

"'xxxxxxxxxx' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string".

my script where the error is causing:

result = db.req.find_one({"_id": ObjectId(sys.argv[1])})

When i check the type() of the sys.argv[1] it says str. I thought wrapping the string around ObjectId should do the trick.

value of sys.argv[1] when printed: '"5902fbdd4d2f430dfe2dded4"'

Anyone one know whats the reason causing the issue?

Thanks in advance.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Resolved:

I removed the JSON.stringify() bit from the passing value in the node.js script. All seems fine now.

over 4 years ago · Santiago Trujillo Report

0

MongoDB allows storing "_id" in "ObjectId" format.

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!