I am doing MongoDB lookups by converting a string to BSON. Is there a way for me to determine if the string I have is a valid ObjectID for Mongo before doing the conversion?
The simplest way to check if the string is a valid Mongo ObjectId is using mongodb module.
const ObjectID = require('mongodb').ObjectID; if(ObjectID.isValid(777777777777777)){ console.log("Valid ObjectID") }