3 SQL database walked into a NoSQL bar. A little while later they walked out because they couldn’t find a table.Anonymous
Today I’m going to connect a MongoDB database to the project. MongoDB is a NoSQL database that is very popular for handling semi-structured and non-structured data. Since my project is still evolving, the data models are not yet finalized. So, I would like the database to be flexible enough to accommodate the changing requirements of the future.
It is very common to be faced with architectural issues once the application starts evolving. Here’s the first one that I encountered in this project: the class
User.java that I defined also contains methods to perform calculations. I also need a
User class to define the model to be used by the database for the interchange of user data.
It is best to separate the calculation methods and data model, and the model class should also ideally be called
User.java. Hence, I’m going to refactor the project such that only the user data model remains in the existing class
User.java and the methods for performing the calculations are in another class. This will make the architecture much neater and easier to manage
The easy way to design the class is first defining the JSON document that will be stored in MongoDB. Then we can use a service like jsonschema2pojo to automatically generate the Java class.
The JSON document and new Java class defining only the attributes of the user to be received as input along with its getters and setter are given in the next section.