Saving enum into mongoDB

inglor picture inglor · Mar 6, 2012 · Viewed 10.2k times · Source

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

public enum SnapshotType {
  EVENT,
  MEMORY
}

Answer

Eve Freeman picture Eve Freeman · Mar 6, 2012

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.