Is there any way to create mongodb like _id strings without mongodb?

fancy picture fancy · May 15, 2012 · Viewed 27.6k times · Source

I really like the format of the _ids generated by mongodb. Mostly because I can pull data like the date out of them client side. I'm planning to use another database but still want that type of _id for my document. How can I create these ids without using mongodb?

Thanks!

Answer

Ruben Stolk picture Ruben Stolk · May 25, 2016

A very easy pseudo ObjectId generator in javascript:

const ObjectId = (m = Math, d = Date, h = 16, s = s => m.floor(s).toString(h)) =>
    s(d.now() / 1000) + ' '.repeat(h).replace(/./g, () => s(m.random() * h))