Should I use ObjectID or uid(implemented by myself) to identify user?

冷暖自知 提交于 2019-12-02 12:32:40

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/17426490/should-i-use-objectid-or-uidimplemented-by-myself-to-identify-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!