Rate this quickstart
- Update Java Driver to 4.2.2.
- Added Client Side Field Level Encryption example.
- Update Java Driver to 4.1.1.
Java is an object-oriented programming language and MongoDB stores documents, which look a lot like objects. Indeed, this is not a coincidence because that's the core idea behind the MongoDB database.
I will use the same repository as usual in this series. If you don't have a copy of it yet, you can clone it or just update it if you already have it:
Here is what a MongoDB document looks like in extended JSON format. I'm using the extended JSON because it's easier to identify the field types and we will need them to build the POJOs.
The first thing we need is a representation of this document in Java. For each document or subdocument, I need a corresponding POJO class.
As you can see in the document above, I have the main document itself and I have an array of subdocuments in the
scoresfield. Thus, we will need 2 POJOs to represent this document in Java:
- One for the grade,
- One for the scores.
In the package
com.mongodb.quickstart.models, I created two new POJOs:
In the Grade class above, I'm using
@BsonPropertyto avoid violating Java naming conventions for variables, getters, and setters. This allows me to indicate to the mapper that I want the
"student_id"field in JSON to be mapped to the
"studentId"field in Java.
Now that we have everything we need, we can start the MongoDB driver code.
I created a new class
com.mongodb.quickstartpackage and here are the key lines of code:
- I need to configure the CodecRegistry to include a codec to handle the translation to and from BSON for our POJOs.
- And I need to add the default codec registry, which contains all the default codecs. They can handle all the major types in Java-like
- I can now wrap all my settings together using
- I can finally initialise my connection with MongoDB.
As you can see in this last line of Java, all the magic is happening here. The
MongoCollection<Grade>I'm retrieving is typed by
Gradeand not by
In the previous blog posts in this series, I showed you how to use CRUD operations by manipulating
MongoCollection<Document>. Let's review all the CRUD operations using POJOs now.
- Here is an insert (create).
- Here is a find (read).
- Here is an update with a
findOneAndReplacereturning the newest version of the document.
- And finally here is a delete.
To start this program, you can use this maven command line in your root project (where the
srcfolder is) or your favorite IDE.
Mapping POJOs and your MongoDB documents simplifies your life a lot when you are solving real-world problems with Java, but you can certainly be successful without using POJOs.
MongoDB is a dynamic schema database which means your documents can have different schemas within a single collection. Mapping all the documents from such a collection can be a challenge. So, sometimes, using the "old school" method and the
Documentclass will be easier.
In the next blog post, I will show you the aggregation framework in Java.