I am new to mongodb and database. Implement a function to make uid and use the local ObjectId. Which is better?

有帮助吗?

解决方案

You should leave ObjectID generation to the clients/drivers. This makes sure that generated IDs are unique among many things, such as time, server and process. Using the standard ObjectID also means that methods implemented by drivers (such as getTimestamp()) work.

However, if you are thinking of using your own type of ID for the _id field (ie, not the standard ObjectID type), then that makes a viable choice. For example, if you want to store information about a twitter user, then using the user's twitter ID as _id value makes perfect sense. Personally, I try to rely on the ObjectID type as little as I have to, as often collections will have a field in each document already that uniquely identifies each document.

其他提示

This depends on three things:

  • Its source
  • Where and how are you using the user ID?
  • Personal opinion.

My personal opinion is that the object ID is good enough, however, getting back to the first and second point.

If this ID comes or is to be used in another database like an SQL database you might find using an incrementing ID a good idea, but SQL and other techs do fully support the object ID in the hexadecimal form.

If this ID is something that can be used much like an account number (think of your account number for car insurance when you phone them up) you might find an object ID too difficult for your users to remember/recounter as such a more human friendly ID might be more applicable here.

So it really depends on how this ID is being used.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top