Saturday, 9 November 2013

Spring Data MongoDB Simple Find example


Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for for new datastores while retaining store-specific features and capabilities. See more about SpringData

In this example we will see how to save and retrieve Customer bean using SpringData and Mongodb.

Prerequisites: Mongodb should be up and running

1. Create a Maven Project
Create a Maven project and add below dependencies

<!-- mongodb java driver -->
<name>Spring Maven MILESTONE Repository</name>

2. Create Customer entity

package com.vinod.model;

public class Customer {
public String name;
public String address;

public Customer(String name, String address) {
super(); = name;
this.address = address;
public Customer() {
public String getName() {
return name;
public void setName(String name) { = name;
public String getAddress() {
return address;
public void setAddress(String address) {
this.address = address;
public String toString() {
return "Customer [name=" + name + ", address=" + address + "]";

3. Spring configuration xml

Place this file in to src/main/resources folder

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""
<!-- Default bean name is 'mongo' -->
<mongo:mongo host="localhost" port="27017" />
<bean id="customermongoTemplate" class="">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="customerdb" />
<constructor-arg name="defaultCollectionName" value="customerCollection" />
<!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
<context:annotation-config />

4. Create a Main class to save Customer details using spring data

package com.vinod.service;

import java.util.List;

import org.springframework.context.ApplicationContext;

import com.vinod.model.Customer;

public class SpringDataFindExample {
    public static void main(String args[]) {
        ApplicationContext ctx = new GenericXmlApplicationContext("application-context.xml");
        MongoOperations mongoOperation = (MongoOperations) ctx.getBean("customermongoTemplate");
        Customer customer = new Customer("Vinod", "Bangalore");"customerDetails", customer);

        // findone
        Customer savedCustomer = mongoOperation.findOne("customerDetails",
                new Query(Criteria.where("name").is("Vinod")), Customer.class);
        System.out.println("savedCustomer : " + savedCustomer);

        // findall

        List<Customer> savedCustomerList = mongoOperation.find("customerDetails",
                new Query(Criteria.where("name").is("Vinod")), Customer.class);
        System.out.println("savedCustomerlist : " + savedCustomerList);


5. Output

savedCustomer : Customer [name=Vinod, address=Bangalore]

savedCustomerlist : [Customer [name=Vinod, address=Bangalore], Customer [name=Vinod, address=Bangalore], Customer [name=Vinod, address=Bangalore]]

6. Done!! Download example