xxxxxxxxxx
const mongoose = require('mongoose');
// Assuming you have an ObjectId
const objectId = new mongoose.Types.ObjectId();
// Convert ObjectId to string
const objectIdString = objectId.toString();
console.log('ObjectId:', objectId);
console.log('ObjectId as String:', objectIdString);
xxxxxxxxxx
The following example calls the valueOf() method on an ObjectId():
ObjectId("507c7f79bcf86cd7994f6c0e").valueOf()
This will return the following string:
507c7f79bcf86cd7994f6c0e
You can confirm the type of this object using the following operation:
typeof ObjectId("507c7f79bcf86cd7994f6c0e").valueOf()