MongoDB Id Generator

JS
S
JavaScript

Tool for generating unique identifiers compatible with MongoDB's ObjectId format, ensuring efficient document indexing and retrieval in MongoDB databases.

1// Function
2const ObjectId = (m = Math, d = Date, h = 16, s = s => m.floor(s).toString(h)) =>
3    s(d.now() / 1000) + ' '.repeat(h).replace(/./g, () => s(m.random() * h))
4
5
6// Regex
7checkForHexRegExp = /^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i
8/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i

Created on 8/16/2019