简体   繁体   中英

How to determine if a string can be used as a MongoDB ObjectID?

I am using Micronaut framework with JAVA and trying to validate the objectId with the below string.

60236833af6a1d49478d2bef   // Valid mongo ObjectId

60236833a46a1d49478d2bef  // Invalid mongo ObjectId

How can I perform either the string has valid objectId or not in Java with MongoDb?

This work for me

import org.bson.types.ObjectId;

if(ObjectId.isValid(value)){
}

You can refer to the ObjectId specification for how they are generated.

Any 24-character hexadecimal string should be convertable to an ObjectId.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM