Frage

Is there a simple built in way to check if a string is a valid ObjectId? I know if I try to construct an ObjectId from an invalid string it throws an exception, is there a way to circumvent the try-catch block?

One possible way would be to check against this regex: ^[0-9a-b]{48}$ Is there something more elegant?

War es hilfreich?

Lösung

There is method ObjectId.isValid(String s): docs

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top