Spring Data MongoDB 是否支持枚举?

Does Spring Data MongoDB support enums?

对于Java枚举类型,我了解到MongoDB有两种解决方案:serialization and using Jackson’s ObjectMapper。 MongoRepository 可以使用这些方法中的任何一种来处理枚举数据类型,还是我必须编写自定义存储库?

是的,Spring数据MongoDB支持枚举。只需在您的领域模型中使用它们。

Spring Data Mongodb 可以使用枚举的名称作为值将枚举序列化为字符串。比方说,它使用文章 http://www.vineetmanohar.com/2010/01/3-ways-to-serialize-java-enums/ 中的第二种方法。恕我直言,这不能认真对待,因为在数据库中存储枚举的唯一正确方法是同一篇文章中的方法 #3,让我引用它:"This approach involves assigning a an explicit user defined value to each enum constant and defining a toValue() and fromValue() methods on the enum to do the serialization and deserialization."。因此,Spring 数据 Mongodb 不支持枚举。