Writing clean code with Lambdas to interact with Mongo Database - Java

Created by: Amuthan Sakthivel

Last updated: July 29, 2022


Functional Programming was introduced in Java 8 but was not yet tapped to its full potential. Since it may be difficult for someone who spent a lot of time in Object-Oriented Language to start to think in a functional style, I will try to give a small taste of what benefit it can give, especially in writing concise, scalable, maintainable, and clean code.


Basic knowledge of Functional Interfaces, Lambdas, and Built-In Functional Interfaces like Consumer, Supplier, Function, and Predicate.

Interaction with MongoDB.

In the test automation framework, we have to interact with the database to get or write some data and we often have to write large amounts of code to achieve this. We will give examples of how that code might look in both the imperative and functional styles.

Reading from Database

In both these styles, we will have the common connector class which looks something like the below:


Imperative style

The DBReader class will need to look something like this to get some value from the database. The code shown below is not complete; we have to add more methods depending on the needs.


The caller code should look something like this:


From the above screengrab, we can also clearly understand that there are a lot of overloaded methods available to fetch documents from the database. Creating a wrapper method for all of them will be very expensive and hard to maintain.

In addition, the caller has to get the document and put some effort to get it in the desired format or parse a particular value from the document. That’s some expensive piece of work!

Functional Style

To get value from the database, DBReader class code will look something like this:


Yes, you are seeing what you think you are seeing. This one method can help to find different values from the database and also it gives plenty of control to use to decide whether he/she wants a list of document or document or document as a string or a specific value from the document.

Here I have used Function<MongoCollection<Document>, ?> function as the argument to the method. This is a functional interface from the java.util.function package that takes some input and returns some output. In this specific case, it takes MongoCollection<Document> and returns a wildcard (indicating it can return anything).

Now let’s see the caller class which has better control than before and decide to fetch the value based on the requirements of this particular task.


DBWriter - Functional Style

Let’s discuss how we can write/update/delete a document in the database.

We can leverage the Consumer interface implementation to create DBWriter class in functional style. From this point on, I will intentionally ignore the imperative style as we have already established that it will be verbose and very hard to maintain.


DBWriter class can be used for all the operations like write, update, and delete in the database. Here is what the caller code looks like:


Taking a step back and looking at everything we’ve done, we find we’ve written a solution that accomplishes the task with a single method and minimal lines of code. . It also gives a lot of control to the caller to decide on what they want to do with the database.

The functional style of programming can reap a lot of benefits if used wisely.

That’s a wrap!