Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums in 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?

like image 552
artch Avatar asked Nov 18 '12 18:11

artch


People also ask

What is enum in MongoDB?

So enum creates a validator and checks if the value is given in an array. E.g: const userSchema = new mongoose. Schema({ userType: { type: String, enum : ['user','admin'], default: 'user' }, }) Follow this answer to receive notifications.

Does MongoDB support enum?

Yes, Spring Data MongoDB supports enums. Just use them in your domain model.

What is a enums database?

In MySQL, an ENUM is a string object whose value is chosen from a list of permitted values defined at the time of column creation. The ENUM data type provides the following advantages: Compact data storage. MySQL ENUM uses numeric indexes (1, 2, 3, …) to represents string values.

What do enums do?

Enum, which is also known as enumeration, is a user-defined data type that enables you to create a new data type that has a fixed range of possible values, and the variable can select one value from the set of values.


1 Answers

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.

like image 82
JohnnyHK Avatar answered Oct 04 '22 05:10

JohnnyHK