存储枚举 MongoDB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28393582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store enum MongoDB
提问by user2248702
I am storing enums for things such as ranks (administrator, moderator, user...) and achievements for each user in my Mongo database. As far as I know Mongo does not have an enum data type which means I have to store it using another type.
我正在为我的 Mongo 数据库中的每个用户存储诸如等级(管理员、版主、用户...)和成就之类的枚举。据我所知 Mongo 没有 enum 数据类型,这意味着我必须使用另一种类型来存储它。
I have thought of storing it using integers which I would assume uses less space than storing strings for everything that could easily be expressed as an integer. Another upside I see of using integers is that if I wanted to rename an achievement or rank I could easily change it without even having to touch the database. A benefit I see for using strings is that the data requires less processing before it is used and is more human readable which could help in tracking down bugs.
我曾想过使用整数来存储它,我认为这比为可以轻松表示为整数的所有内容存储字符串使用更少的空间。我看到使用整数的另一个好处是,如果我想重命名成就或排名,我可以轻松更改它,甚至无需接触数据库。我看到使用字符串的一个好处是数据在使用前需要较少的处理,并且更具人类可读性,这有助于跟踪错误。
Are there any better ways of storing enums in Mongo? Is there an strong reason to use either integers or strings? (trying to stay away from a which is better question)
有没有更好的方法在 Mongo 中存储枚举?是否有充分的理由使用整数或字符串?(试图远离哪个更好的问题)
回答by mnemosyn
TL;DR: Strings are probably the saferchoice, and the performance difference should be negligible. Integers make sense for huge collections where the enum must be indexed. YMMV.
TL;DR:字符串可能是更安全的选择,性能差异应该可以忽略不计。整数对于必须索引枚举的大型集合有意义。天啊。
I have thought of storing it using integers which I would assume uses less space than storing strings for everything that could easily be expressed as an integer
我曾想过使用整数来存储它,我认为这比为所有可以轻松表示为整数的东西存储字符串使用更少的空间
True.
真的。
other upside I see of using integers is that if I wanted to rename an achievement or rank I could easily change it without even having to touch the database.
我看到的使用整数的另一个好处是,如果我想重命名成就或排名,我可以轻松更改它,甚至无需接触数据库。
This is a key benefit of integers in my opinion. However, it also requires you to make sure the associated values of the enum
don't change. If you screw that up, you'll almost certainly wreak havoc, which is a huge disadvantage.
在我看来,这是整数的一个主要好处。但是,它还要求您确保 的关联值enum
不会更改。如果你搞砸了,你几乎肯定会造成严重破坏,这是一个巨大的劣势。
A benefit I see for using strings is that the data requires less processing before it is used
我看到使用字符串的一个好处是数据在使用前需要更少的处理
If you're actually using an enum data type, it's probably some kind of integer internally, so the integer should require less processing. Either way, that overhead should be negligible.
如果您实际上使用的是 enum 数据类型,则它内部可能是某种整数,因此该整数应该需要较少的处理。无论哪种方式,该开销都应该可以忽略不计。
Is there an strong reason to use either integers or strings?
是否有充分的理由使用整数或字符串?
I'm repeating a lot of what's been said, but maybe that helps other readers. Summing up:
我重复了很多已经说过的话,但也许这对其他读者有所帮助。加起来:
- Mixing up the enum value map wreaks havoc. Imagine your
Declined
states are suddenly interpreted asAccepted
, becauseDeclined
had the value '2' and now it'sAccepted
because you reordered the enum and forgot to assign values manually... (shudders) - Strings are more expressive
- Integers take less space. Disk space doesn't matter, usually, but index space will eat RAM which is expensive.
- Integer updates don't resize the object. Strings, if their lengths vary greatly, might require a reallocation. String padding and padding factor should alleviate this, though.
- Integers can be flags (not yet queryable (yet), unfortunately, see SERVER-3518)
- Integers can be queried by
$gt
/$lt
so you can efficiently implement complex$or
queries, though that is a rather arcane requirement and there's nothing wrong with$or
queries...
- 混淆枚举值映射会造成严重破坏。想象一下,您的
Declined
状态突然被解释为Accepted
,因为Declined
其值为 '2' 而现在是Accepted
因为您重新排序了枚举并忘记手动分配值......(不寒而栗) - 字符串更具表现力
- 整数占用更少的空间。磁盘空间通常无关紧要,但索引空间会占用昂贵的 RAM。
- 整数更新不会调整对象的大小。字符串,如果它们的长度变化很大,可能需要重新分配。不过,字符串填充和填充因子应该可以缓解这种情况。
- 整数可以是标志(尚不可查询(尚),不幸的是,请参阅SERVER-3518)
- 可以通过
$gt
/查询整数,$lt
因此您可以有效地实现复杂的$or
查询,尽管这是一个相当神秘的要求,并且$or
查询没有任何问题......