Setup and Configure Spring Data MongoDB

I recently start working on creating a new app and have to design a database schema for loading data (in csv files) into a database. I end up using MongoDB instead of relational databases like MySQL as the Document database schema of MongoDB fits well with my data. MongoDB provides its own client driver but as always, there is a Spring framework, in this case Spring Data MongoDB, to make things better. I will outline the steps I took to setup and configure a project for developing application using Spring Data MongoDB.

Download and Install MongoDB

Download MongoDB here and following the installation and setup instructions.

Getting Spring Data MongoDB

Add the following Maven dependency:

<!– Spring Data MongoDB –>

Update Spring config

You can use either annotation or xml files to configure Spring Data MongoDB. Below is my spring config xml file.

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”;
xmlns:xsi=”; xmlns:mongo=”;

<beans profile=”local”>
<context:property-placeholder location=””/>

<mongo:mongo host=”${}” port=”${mongo.port}”>

<bean id=”mongoTemplate” class=””>
<constructor-arg ref=”mongo” />
<constructor-arg value=”${mongo.dbname}” />

<mongo:repositories base-package=”” />


My file for a local environment looks like below

Implement the domain objects

You develop your POJOs with annotation in a similar way as in JPA/Hibernate beans. For example (from Getting Started guide of Spring Data MongoDB),

public class Customer {

private String id;

private String firstName;
private String lastName;

Note Spring Data does most work by convention. You don’t need to add the annotation @Document to the class – it will map the entity into a collection of name customer. Here I add the annotation to redirect it to a different collection called mycustomer. Similarly, Spring Data will look for a property id for use as id of the collection and will create one if it could not find one and there is no @Id annotation found in the class. The properties firstName and lastName are mapped into properties of same names in the database.

Implement the Repository

Again from Spring Data MongoDB’s own getting start guide

public interface CustomerRepository extends MongoRepository<Customer, String> {

public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);


I define an interface here by extending MongoRepository. Together with my setup in Spring config (below), Spring Data will create the implementation class to support standard Crud operations as well as simple query methods like the ones defined above.

<mongo:repositories base-package=”” />

Note the method signature in the interface is important:

  • The first method findByFirstName will return the first customer found while the other findByLastName will return all customers with input lastName.
  • The method name is used to define the query to use. It is possible to search using more than one properties. For example, to search customer with given first and last name:

Customer findByFirstNameAndLastName(String firstName, String lastName);

  • You can also sort results by adding an input argument with class Sort. For example,

public List<Customer> findByLastName(String lastName, Sort sort);


Now we are all set for running some tests.

public class CustomerRepositoryTest {

protected CustomerRepository repository;

public void before() {
// save a couple of customers Customer(“Alice”, “Smith”)); Customer(“Bob”, “Smith”));

public void testGetByLastName() {
List<Customer> customers = repository.findByLastName(“Smith”);
assertEquals(2, customers.size());


One last thing. Turn on the debug mode in log4j, When running the above, you should see something like below

DEBUG: 17 Nov 2013 12:35:58,916 (main) – Getting Mongo Database name=[testdb]
DEBUG: 17 Nov 2013 12:35:59,109 (main) – Remove using query: { } in collection: mycustomer.
DEBUG: 17 Nov 2013 12:35:59,116 (main) – Saving DBObject containing fields: [_class, _id, firstName, lastName]
DEBUG: 17 Nov 2013 12:35:59,117 (main) – Getting Mongo Database name=[testdb]
DEBUG: 17 Nov 2013 12:35:59,119 (main) – Saving DBObject containing fields: [_class, _id, firstName, lastName]
DEBUG: 17 Nov 2013 12:35:59,119 (main) – Getting Mongo Database name=[testdb]
DEBUG: 17 Nov 2013 12:35:59,138 (main) – Created query Query: { “lastName” : “Smith”}, Fields: null, Sort: null
DEBUG: 17 Nov 2013 12:35:59,145 (main) – find using query: { “lastName” : “Smith”} fields: null for class: class com.madman.abs.domain.Customer in collection: mycustomer

That’s it.

About Raymond Lee
Professional Java/EE Developer, software development technology enthusiast.

Comments are closed.