Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store enum MongoDB

Tags:

enums

mongodb

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.

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)

like image 877
user2248702 Avatar asked Feb 08 '15 11:02

user2248702


People also ask

How is enum stored in MongoDB?

Steps: Create and register a codec provider with Mongo Code Registry which Mongo uses to determine which Enum decoder to use a Java Enum value. Create and register Enum decoder for ProcessType. Create and register Enum with DB.

Does MongoDB support enum?

Yes, Spring Data MongoDB supports enums.

Should you store enum in database?

By keeping the enum in your database, and adding a foreign key on the table that contains an enum value you ensure that no code ever enters incorrect values for that column. This helps your data integrity and is the most obvious reason IMO you should have tables for enums.

What is enum in Mongoose schema?

Mongoose has several inbuilt validators. Strings have enum as one of the validators. So enum creates a validator and checks if the value is given in an array. E.g: var userSchema = new mongooseSchema({ userType: { type: String, enum : ['user','admin'], default: 'user' }, }) Follow this answer to receive notifications.


1 Answers

TL;DR: Strings are probably the safer choice, and the performance difference should be negligible. Integers make sense for huge collections where the enum must be indexed. YMMV.

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.

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.

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 as Accepted, because Declined had the value '2' and now it's Accepted 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...
like image 143
mnemosyn Avatar answered Oct 11 '22 14:10

mnemosyn