我应该使用ObjectID还是uid(由我自己实现)来标识用户? [英] Should I use ObjectID or uid(implemented by myself) to identify user?

查看:122
本文介绍了我应该使用ObjectID还是uid(由我自己实现)来标识用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 mongodb 和数据库的新手。
实现一个函数以制作 uid 并使用本地的 ObjectId
哪个更好?

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

推荐答案

您应该将ObjectID生成留给客户端/驱动程序。这样可以确保生成的ID在许多事物(例如时间,服务器和进程)之间是唯一的。使用标准的ObjectID还意味着驱动程序实现的方法(例如getTimestamp())可以工作。

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.

但是,如果您正在考虑使用自己的 _id字段的ID类型(即不是标准的ObjectID类型),那么这是一个可行的选择。例如,如果您要存储有关Twitter用户的信息,则将用户的Twitter ID用作_id值是很合理的。就我个人而言,我尽量减少对ObjectID类型的依赖,因为在每个文档中,集合通常已经具有一个可以唯一标识每个文档的字段。

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.

这篇关于我应该使用ObjectID还是uid(由我自己实现)来标识用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆