String to object id

Hello,

I am trying to change string value to object Id. I tried several ways, but couldn’t get what I want.

const { ObjectId } = require(‘mongodb’);

const stringValue = ‘999’; // Your string value

try {
const objectId = ObjectId(stringValue);
console.log(objectId); // The converted ObjectId
} catch (error) {
console.error(‘Invalid string value:’, error);
}

Receiving an error as - MongoDB Node.js Driver is not supported.
I added mongodb dependency to my project.

Please do let me know solution to fix this issue.
Thanks in advance.

Hello @sunita_kodali ,

Can you please share a few more relevant details for me to understand your issue better?

  • MongoDB Version
  • Exact error that you are getting
  • OS and version
  • Node.js Version
  • Driver version

Kindly refer below links for more information on MongoDB Node.js driver

Regards,
Tarun

Will go over the articles. Thank you.

1 Like