MongoDB is one of my favorite data stores when it comes to storing document-based JSON data. Communicating with MongoDB with JVM languages can be done in a lot of ways. I thought it would be a nice exercise to take 4 of the most popular solutions and implement a simple use case in each of those solutions. The use case: create a REST service that can get a list of cities and get the nearest city for a given city with the distance to that city.
The 4 approaches I’ll compare are using the standard MongoDB Java Driver, Jongo, Morphia and finally Spring Data for MongoDB. All the code is written in Groovy for brevity and I’ll be using Spring Boot to minimize boilerplate code to provide the REST layer.
Java EE + MongoDb with Apache TomEE and Jongo Starter Project
Know MongoDB and Java EE, but you don’t know exactly how to integrate both of them? Do you read a lot about the topic but you have not found a solution which fits this purpose? This starter project is for you:
You will learn how to use MongoDB and Java EE in a fashion way without having to depend on Spring Data MongoDB framework but with “similar” basic features.
The only thing better than a Maven archetype is a repository you can fork with everything already setup. Skip the documentation and just fork-and-code. This starter project contains:
The example is pretty simple, we want to store colors inside a MongoDB collection.
Our POJO is like: