将MongoDB中的_id类型更改为整数是否不好? [英] Is it bad to change _id type in MongoDB to integer?

查看:348
本文介绍了将MongoDB中的_id类型更改为整数是否不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB使用_id的ObjectId类型.

MongoDB uses ObjectId type for _id.

如果我将_id设为递增整数会不好吗?

Will it be bad if I make _id an incrementing integer?

(如果您有兴趣,请使用宝石)

(With this gem, if you're interested)

推荐答案

不,这一点也不坏,实际上,内置的ObjectId在索引中相当大,因此,如果您认为自己有更好的选择,那么非常欢迎将_id字段的默认值更改为任何值.

No it isn't bad at all and in fact the built in ObjectId is quite sizeable within the index so if you believe you have something better then you are more than welcome to change the default value of the _id field to whatever.

但是,这是一个很大的 ,因此,当决定脱离默认公式化的ObjectId时,需要考虑一些注意事项,尤其是在使用自动递增的_ids时,如下所示: ="http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/#auto-increment-counters-collection" rel ="noreferrer"> http://docs.mongodb.org /manual/tutorial/create-an-auto-incrementing-field/#auto-increment-counters-collection

But, and this is a big but, there are some considerations when deciding to move away from the default formulated ObjectId, especially when using the auto incrementing _ids as shown here: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/#auto-increment-counters-collection

多线程并不是什么大问题,因为findAndModify和原子锁实际上可以解决这个问题,但是随后您遇到了第一个问题. findAndModify既不是最快的功能,也不是最轻的功能,并且定期使用它会发现性能显着下降.

Multi threading isn't such a big problem because findAndModify and the atomic locks can actually take care of that, but then you just hit into your first problem. findAndModify is not the fastest function nor the lightest and there have been significant performance drops noticed when using it regularly.

即使没有findAndModify,您也必须考虑自己执行此操作的开销.对于您拥有的每个插入物,您都需要做一个额外的查询.具有唯一ID的图像,您每次要插入时都要查询其唯一性,最终插入率将下降为爬网,并且您的锁将建立起来.

You have also got to consider the overhead of doing this yourself anyway, even without findAndModify. For every insert you have you will need to do an extra query. Image having a unique id that you have to query the uniqueness of every time you wanted to insert, eventually your insert rate will drop to a crawl and your lock will build up.

当然,ObjectId确实非常擅长于唯一性,而不必在插入之前通过触摸数据库来检查或制定其自身的唯一性,因此它没有这种开销.

Of course the ObjectId is really good at being unique without having to check or formulate its own uniqueness by touching the database prior to insertion, hence it doesn't have this overhead.

话虽这么说,这并不是世界上最糟糕的主意,如果您认为它适合您的情况,那么就去做吧,但是请记住,如果您不需要自动递增ID,可能会给您带来负担.

That being said it is not the worst idea in the world and if you think it suites your scenario then go for it but bare in mind that if you don't require the auto incrementing id it could be a burden to you.

这篇关于将MongoDB中的_id类型更改为整数是否不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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