Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving enum into mongoDB

Is there a way to save enum into the mongoDB? I want to save something like:

public enum SnapshotType {
  EVENT,
  MEMORY
}
like image 851
inglor Avatar asked Mar 06 '12 17:03

inglor


People also ask

Does Mongodb support enum?

Enumerations, also known as enums, are not supported natively in the Java SDK.

How is enum stored in database?

First of all, in order to save enum values in a relational database using JPA, you don't have to do anything. By default, when an enum is a part of an entity, JPA maps its values into numbers using the ordinal() method. What it means is that without customizations JPA stores enum value as numbers.

What is an enum TypeScript?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.


2 Answers

I assume you mean saving an enum value into a collection.

Basically, you just add it into your entity model, like so:

@Document(collection = "MyEntity ")
public class MyEntity {
   public SnapshotType snapshotType;
}

It will store it as a string in mongo, and automagically convert when you read it out.

like image 104
Eve Freeman Avatar answered Sep 19 '22 18:09

Eve Freeman


Just save the result. There are no schemas in mongo.

like image 22
dicarsio Avatar answered Sep 21 '22 18:09

dicarsio