We used cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. What For?

« Back to Blogs

Liferay Spring MVC MongoDB Portlet

Nowadays MongoDB is most widely used NoSQL database. Like other database systems, MongoDB also have different drivers API for different platforms like java and php which allows application to communicate with MongoDB.


MongoDB provides Mongo Java Driver which allows java applications to communicate with MongoDB. This is pure java implementation driver we can use in any java application to communicate with MongoDB.


To perform database operations with MongoDB, Spring provides Spring Data MongoDB java library. Spring Data MongoDB uses Mongo Java Driver in their implementation.


Here, I am going to show how to establish a connection and perform database operation in MongoDB in a Spring MVC Liferay portlet. I have used liferay 6.2 CE GA4 Portal for this example.


Let me start with creating Spring MVC Liferay Portlet that uses a JAR File for database operation. This component contains the code that interacts with MongoDB. You can find the component JAR inside portlet WAR WEB-INF/lib directory.


Prerequisite :

  • Knowledge of Spring MVC Liferay portlet and basic knowledge of MongoDB is required to understand this blog.

  • I am assuming that MongoDB is running on the system and ready to use.

To install MongoDB refer : MongoDB installation guide


Step : 1 - Add dependency for MongoDB in your pom.xml.








Step : 2 - Create Entity Class.

In this example, I am going to persist Employee object to MongoDB.


Employee class should look like this :



public class Employee implements Serializable {



private String id;

private String name;

private String address;

private String age;


//getter, setter, toString, Constructors



Here you can see that, we used some Spring specific annotations like @Id and @Document.

@Id will be mapped to the '_id' column in database. A property or field without @Id annotation but named id will be mapped to the '_id' column in database. The @Document annotation identifies an object that is going to be persisted in MongoDB. Now, as we have a persistable object, we can move on to the real interaction.

Step : 3 - Load MongoDB configurations included in spring configuration file.

For connectivity with MongoDB, we can make use of Spring Data MongoTemplate class, for that load configuration file from web.xml







Now add mongodb configuration to applicationContext.xml.

<!-- configuration for connection to MongoDB -->

<mongo:mongo host="localhost" port="27017" />

<mongo:db-factory dbname="yourdbname" />


<!-- MongoTemplate for quering the documents in the database -->

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />



Above mentioned lines will load MongoTemplate bean and we are able to use it by just autowiring it.


MongoTemplate is the central class of the Spring’s MongoDB support providing a rich feature set to interact with the database. The template offers a convenience operations to create, update, delete and query for MongoDB documents and provides a mapping between your domain objects and MongoDB documents.


Step : 4 - Perform Database operation using MongoTemplate.


Following is the EmployeeDAOImpl class from this component.

Here is how you can autowire it and use it for database operations :

public class EmployeeDAOImpl implements IEmployeeDAO {



private MongoTemplate mongoTemplate;



public void save(Employee employee) {





public Employee getById(String id) {

return mongoTemplate.findById(id, Employee.class);




public List<Employee> getAll() {

return mongoTemplate.findAll(Employee.class);




public void delete(Employee employee) {





public void update(Employee employee) {






Download :



For more implementation details or support you may contact us at [email protected].

contact-us Request a callback WhatsApp