There’re reasons why MongoDB is getting so popular.
First of all, it’s open source and free to use. Secondly, no SQL queries are needed. Third, adding, updating, and deleting with java is easy. Lastly, we can easily convert Java object to JSON and store it in MongoDB. But there are some limitations, such as joining tables and managing the memory usage.
Now let’s see some examples with Java.
In this exercise, we use a Java Sprintboot and Web UI to read, create, read, and delete data from MongoDB.
The functionality of our sample app includes
Create, edit and delete contacts
If a user clicks the New Contact button a form is displayed
We can provide a Name and Email for our new contacts
We can save the contacts
Web UI to visualize contacts and create new Contacts
Web UI to add a contact name and email and Save it
We will reuse the code from our last Springboot example on the link below
Step 1: We add a few additional dependencies in the pom.xml
Step 2: We create a Contact class
Step 3: We create a controller class
Specify a few global variables such as the name of our database, the name of our database server, the port and the name of the collection we are storing in the NoSQL database.
Step 4: We create the Contact HTML page
Add buttons to create new, edit and delete buttons and add the JavaScript code to execute the corresponding backend method.
Step 5: We implement the Controller class to read the contacts from our MongoDB
Step 6: We implement the New Contact HTML page
Step 7: We add a New method to the Controller class
Step 8: We implement the ContactSave method in the Controller class
Keep in mind that reading all the elements can be time consuming and you might chose a different approach for your application.
Step 9: In the Controller class we add a ContactDelete method.
Step 10: We validate the data stored in Mongo Database.
We use MongoDB Compass: https://www.mongodb.com/try/download/shell
Keep up the great work! Happy Performance Engineering!
Comments