Use official identification card id or generated id's from MongoDB for identifying users?
database-schema, mongodb
Solution
Uh, this is the old "surrogate key vs. non-surrogate (natural) key" discussion. This is a bit of a flame war...
I think surrogate keys (i.e. `ObjectIds`) are the way to go. The key objections to the "natural" key (the official id card id) are these:
- You can't verify them. If the user entered a wrong number (but with a valid format), what do you do? Since primary keys can't be changed, you'll have to re-insert a new object and fix all links to the user. Ugly.
- You have no control of them. If the government decides to re-issue a number, you're out of luck
- Their format can change over time, which isn't a big problem for MongoDB but might be a problem in the code / in the API
- The natural key might expose information to the outside that shall remain secret for some/many users of the system. Suppose amazon would use your id card's number as a public identifier of your account.
...and a MongoDB-specific argument
- The `ObjectId` has a number of properties: it carries a timestamp and it's monotonic. Natural keys usually can't make such guarantees
Problem
I am designing the schema for a simple MongoDB database where there will be a collection for customers. Natural way of identifying customer will be use their official identification card id (6 numbers and a letter, unique for each person in the country). Another way of doing will be letting MongoDB to choose the _id field value and using the card id as another field. Any suggestion of advantages / disadvantages of choosing one string with 6 numbers and a letter as _id, or choosing and ObjectId created by Mongo?