For Java enum type, I learn that there are two solutions for MongoDB: serialization and using Jackson’s ObjectMapper. Can the MongoRepository work with an enum data type with either of those approaches or I have to write a customized repository?
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.
The Spring framework provides powerful connectors to easily perform database operations with MongoDB. Data is stored as BSON objects in MongoDB making data retrieval easy. For this Spring Boot and MongoDB example tutorial, we are only concerned with the Persistence and Database layers.
Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for new datastores while retaining store-specific features and capabilities.
Spring Boot is the easiest way to spin a spring project quickly and MongoDB is the most popular NoSQL database.
Yes, Spring Data MongoDB supports enums. Just use them in your domain model.
Spring Data Mongodb can serialize enum into string using enum's name as value. Let's say, it uses the second approach from the article http://www.vineetmanohar.com/2010/01/3-ways-to-serialize-java-enums/. IMHO this can't be taken seriously, because the only correct way to store enums in the database is the approach #3 from the same article, let me cite it: "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.". So, Spring Data Mongodb does not support enums.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With