Constantine Samoilenko
Constantine Samoilenko

Reputation: 343

Modify mongodb ID policy

I'm writing a system with registration. And by device capabilities the requirement is to have 16 bytes for user id. Also, I know that mongo creates the ObjectID for each object inserted, which is unique at least in the collection. And this id is 12 bytes long. I was wondering - is it possible to change config somewhere in mongo to make it generate 16 bytes id? I thought about padding 12 bytes ids with 4 zero bytes, it is a solution, but it's brute force imo. Is there a better way?

Upvotes: 0

Views: 122

Answers (1)

xdg
xdg

Reputation: 2995

There is no server side configuration to change the format of ObjectIDs, but you can provide whatever _id you want yourself within your application. For inserts, drivers generate the _id for you locally anyway behind the scenes on insert; the server only generates it for upsert operations.

As long as you don't depend on upsert, the best thing to do is to generate your own _id fields. For 16-bytes, you could use a UUID generator, for instance.

Upvotes: 1

Related Questions