Aman Gupta
Aman Gupta

Reputation: 45

Mongodb Object Id

Sometimes MongoDB creates object id with only numeric and sometimes is alphanumeric can we add a validation pattern for objectId from MongoDB JsonSchema validation or PartialFilterExpression? I need that object id should always pass this pattern /^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i for _id

Upvotes: 1

Views: 74

Answers (1)

Pawan Yadav
Pawan Yadav

Reputation: 89

Hello @Aman to check if it exists a valid mongodb object you can test with following regex.

const regex = /^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i;
console.log(regex.test(`${mongodb_id}`)) //returns true if valid id else returns false

Upvotes: 0

Related Questions