MongoDB中的枚举 [英] Enums in MongoDB

查看:832
本文介绍了MongoDB中的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MongoDB集合中存储枚举值。我可以有效地将其存储为字符串吗?与枚举值作为ints相比,它会影响索引性能吗? Mongo索引器是否在字符串值只包含少量固定选项的情况下优化字符串索引,以达到与排序整数查询索引类似的速度?

解决方案

将MongoDB中的枚举值存储为字符串是完全正确的,是的,如果您索引字段,我希望性能可以与索引整数查询相当。这比使用整数更有表现力。



唯一真正的缺点是,如果你的枚举字符串有点长,那么它们会占用更多的空间,但这是一个很小的问题。


I want to store enum values in MongoDB collection. Can I effectively store them as strings? Will it affect index performance in comparison with enum values as ints? Does Mongo indexer optimize string indexes in the case when they contain only few fixed options as string values, to achieve speed similar to querying index by sorted integer?

解决方案

Storing enum values in MongoDB as strings is perfectly fine, and yes, if you index the field I'd expect the performance to be comparable to indexed integer queries. It's certainly more expressive than using integers.

The only real downside is that they'll take more space if your enum strings are somewhat long, but that's a pretty trivial concern.

这篇关于MongoDB中的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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