mongodb中_id的长度是否有限制 [英] Is there a limit on the length of _id in mongodb

查看:1323
本文介绍了mongodb中_id的长度是否有限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我已经正在接管一个应用程序(原始工程师将离开),该应用程序充当某些相对较慢的后端服务的缓存层.因为它是RESTful样式的URL,所以每个URL都是唯一的.该应用程序使用MongoDb作为缓存的存储,并使用哈希值作为缓存.尽管哈希码应该是非常独特的,但是它不是唯一的.

Background: I have am taking over an application (original engineer is leaving) that act as caching layer of some relatively slow backend services. Because it's RESTful style URL, each URL is unique. The application uses MongoDb as the storage for cache, and uses hash value as the cache. Although hash code should be pretty unique, but it is not unique.

问题:有人告诉我使用哈希码(而不是url)的原因是因为MongoDb的_id字段有长度限制,但是我找不到关于它的任何文档.我在MongoDb文档中可以找到的唯一内容是"_id字段可以是数组以外的任何东西,只要它是唯一的". MongoDb的_id字段是否有长度限制,这是真的吗?如果可以,限制大小是多少?

Question: I was told the reason to use hash code (instead of the url) was because MongoDb's _id field has limit on length, but I can't find any document on that. All I can find in the MongoDb documentation is "_id field can be anything other than array as long as it's unique". Is it true that MongoDb's _id field has length limit? If so what is the limit size?

该应用程序是用Java编写的.哦,我是MongoDb的新手.

The application is written in Java. Oh, and I am new to MongoDb.

推荐答案

要建立索引的字段的长度有一个限制,即1024个字节.这是对索引条目大小的限制,而不是文档字段大小的限制,该大小限制为〜16MB(完整文档的最大大小).

There is a limit to the length of the field to be indexed, which is 1024 bytes. That's a limitation on index entry size rather than document field size which are limitated at ~16MB (the maximum size of a complete document).

出于性能方面的原因,您实际上并不希望索引字段的字段值较大,因为与此类较大值的比较要慢得多.还要记住,每个索引都维护着被索引值的副本,因此将需要大量的内存.反过来,这意味着要更频繁地访问磁盘以在内存中交换虚拟内存页面,这又会对性能产生负面影响.

For performance reasons you do not really want large field values for indexed fields as comparisons against such big values are considerably slower. Also remember that every index maintains copies of the values being indexed so it would require significant amounts of memory. That in turns means more frequent disk access to swap virtual memory pages in and out of memory which again has a negative impact on performance.

是的,限制为800个字节.

So yes, limited to 800 bytes.

这篇关于mongodb中_id的长度是否有限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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