Spring Data MongoDB hello world example

In this tutorial, we show you how to use “SpringData for MongoDB” framework, to perform CRUD operations in MongoDB, via Spring’s annotation and XML schema.

Updated on 1/04/2013
Article is updated to use latest SpringData v 1.2.0.RELEASE, it was v1.0.0.M2.

Tools and technologies used :

  1. Spring Data MongoDB – 1.2.0.RELEASE
  2. Spring Core – 3.2.2.RELEASE
  3. Java Mongo Driver – 2.11.0
  4. Eclipse – 4.2
  5. JDK – 1.6
  6. Maven – 3.0.3

P.S Spring Data requires JDK 6.0 and above, and Spring Framework 3.0.x and above.

1. Project Structure

A classic Maven’s style Java project directory structure.

SpringData-helloworld-example

2. Dependency

The following libraries are required :

spring-data-mongodb
Currently, the “spring-data-mongodb” jar is only available in “http://maven.springframework.org/milestone“, so, you have to declare this repository also.

Updated on 13/09/2012
spring-data-mongodb is available at the Maven central repository, Spring repository is no longer required.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong.core</groupId>
	<artifactId>SpringMongoDBExample</artifactId>
	<packaging>jar</packaging>
	<version>1.0</version>
	<name>SpringMongoExample</name>
	<url>http://maven.apache.org</url>

	<dependencies>

		<!-- Spring framework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>

		<!-- mongodb java driver -->
		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>2.11.0</version>
		</dependency>
		
		<!-- Spring data mongodb -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-mongodb</artifactId>
			<version>1.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2.2</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

3. Spring Configuration, Annotation and XML

Here, we show you two ways to configure Spring data and connect to MongoDB, via annotation and XML schema.

Note
Refer to this official reference Connecting to MongoDB with Spring.

3.1 Annotation
Extends the AbstractMongoConfiguration is the fastest way, it helps to configure everything you need to start, like mongoTemplate object.

SpringMongoConfig.java

package com.mkyong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;

@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {

	@Override
	public String getDatabaseName() {
		return "yourdb";
	}

	@Override
	@Bean
	public Mongo mongo() throws Exception {
		return new MongoClient("127.0.0.1");
	}
}

Alternatively, I prefer this one, more flexible to configure everything.

SpringMongoConfig1.java

package com.mkyong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.MongoClient;

@Configuration
public class SpringMongoConfig1 {

	public @Bean
	MongoDbFactory mongoDbFactory() throws Exception {
		return new SimpleMongoDbFactory(new MongoClient(), "yourdb");
	}

	public @Bean
	MongoTemplate mongoTemplate() throws Exception {
		
		MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
				
		return mongoTemplate;
		
	}

}

And load it with AnnotationConfigApplicationContext :


    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations)ctx.getBean("mongoTemplate");

3.2 XML Schema

SpringConfig.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<mongo:mongo host="127.0.0.1" port="27017" />
	<mongo:db-factory dbname="yourdb" />

	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
	</bean>

</beans>

And include it with Spring’s GenericXmlApplicationContext :


   ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
   MongoOperations mongoOperation = (MongoOperations)ctx.getBean("mongoTemplate");	
So, XML or Annotation?
Actually, both are doing the same thing, it’s just based on personal preferences. Personally, I like XML to configure things.

4. User Model

An User object, annotated @Document – which collection to save. Later, we show you how to use Spring data to bind this object to / from MongoDB.

User.java

package com.mkyong.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "users")
public class User {

	@Id
	private String id;

	String username;

	String password;
	
	//getter, setter, toString, Constructors

}

5. Demo – CRUD Operations

Full example to show you how to use Spring data to perform CRUD operations in MongoDB. The Spring data APIs are quite clean and should be self-explanatory.

App.java

package com.mkyong.core;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.mkyong.config.SpringMongoConfig;
import com.mkyong.model.User;
//import org.springframework.context.support.GenericXmlApplicationContext;

public class App {

    public static void main(String[] args) {

	// For XML
	//ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");

	// For Annotation
	ApplicationContext ctx = 
             new AnnotationConfigApplicationContext(SpringMongoConfig.class);
	MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

	User user = new User("mkyong", "password123");

	// save
	mongoOperation.save(user);

	// now user object got the created id.
	System.out.println("1. user : " + user);

	// query to search user
	Query searchUserQuery = new Query(Criteria.where("username").is("mkyong"));

	// find the saved user again.
	User savedUser = mongoOperation.findOne(searchUserQuery, User.class);
	System.out.println("2. find - savedUser : " + savedUser);

	// update password
	mongoOperation.updateFirst(searchUserQuery, 
                         Update.update("password", "new password"),User.class);

	// find the updated user object
	User updatedUser = mongoOperation.findOne(searchUserQuery, User.class);

	System.out.println("3. updatedUser : " + updatedUser);

	// delete
	mongoOperation.remove(searchUserQuery, User.class);

	// List, it should be empty now.
	List<User> listUser = mongoOperation.findAll(User.class);
	System.out.println("4. Number of user = " + listUser.size());

    }

}

Output


1. user : User [id=516627653004953049d9ddf0, username=mkyong, password=password123]
2. find - savedUser : User [id=516627653004953049d9ddf0, username=mkyong, password=password123]
3. updatedUser : User [id=516627653004953049d9ddf0, username=mkyong, password=new password]
4. Number of user = 0

Download Source Code

Download it – SpringMongoDB-HelloWorld-Example.zip (24 KB)

References

  1. Spring data for MongoDB
  2. Connecting to MongoDB with Spring
  3. Oreilly Spring data mongodb tutorial
  4. Another good Spring data mongodb tutorial

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
naveen
Guest
naveen

hello Mkyong,

Thanks . I had put save operation in loop and after some time it stops saving the data and application stops . I think there are too many connections getting open.

how can we close the connection using mongoOperation . I saw this in stackoverflow but could not implement ->
replacing MongoOperations with the implementation MongoTemplate and then on the MongoTemplate call mongoTemplate.getDb().getMongo().close();

could you pls help ?

xiaoqin
Guest
xiaoqin

very nice?it is very help for me!!

Flat R
Guest
Flat R

gratitude to Mkyong – The only working Spring-MongoDB example I could find.

User
Guest
User

Hi do you have an example with jndi mongodb with java config? I would like to keep mongo db details outside project in tomcat.

User
Guest
User

forgot to mention I want spring java rest application where I am using mongodb.

Carol
Guest
Carol

nice!

Charlie
Guest
Charlie

Thanks for this!
One thing I noticed, there is a typo in the xml configuration.

The host and port are not wired into the mongoTemplate.
It’s only an ussue if your mongo db is not running locally as the template uses the default host and port for mongo.

Shrivats Rajagopalan
Guest
Shrivats Rajagopalan

Could you please add a sample for finding distinct values?

Ved Prakash Mishra
Guest
Ved Prakash Mishra

hello mkyong,

would you please share spring data with spring mvc application ?

Asvin Kumar
Guest
Asvin Kumar

Is it possible to handle concurrency? Throw an exception when multiple users trying to update same?

Maxi Wu
Guest
Maxi Wu

this is really confusing, SpringMongoConfig1.java do not specify any connection parameter, should at least put “127.0.0.1” to clarify thing.

Maxi Wu
Guest
Maxi Wu

This somehow seems anti pattern to me. Since one of the most important feature using MongoDB is to free from predefined schema. We should be able to CRUD like JSONObject to MongoDB without any POJO class, right?

rookie
Guest
rookie

How can we enable SSL connection to a MOngo DB through Spring, can you please explain Mr. Mkyong

Hari
Guest
Hari

how do I make this connection singleton? I’m seeing tons of connections from my app to the mongo db and they never close.

Hari
Guest
Hari

found the issue. Do not load the mongoConfig with AnnotationConfigApplicationContext as shown in the example. Instead use @Autowired to let spring inject it for you. AnnotationConfigApplicationContext creates a new virtual container within and hence it keeps creating a new connection to mongodb which leaves lots of mongodb connections lying around. Autowiring solves this problem by reusing the connections. Good luck!

@Autowired
private MongoDBConfig mongoConfig;

public MongoApplication mongoAppData (String appName) {

try {

MongoOperations mongoOperation = mongoConfig.mongoTemplate();

logger.info(appName);

Query searchAppQuery = new Query(Criteria.where(“canonical_name”).is(appName));

MongoApplication newapp = mongoOperation.findOne(searchAppQuery, MongoApplication.class);

return newapp;

} catch (Exception e) {

logger.error(“Error retrieving app data”);

return null;

}

}

Rajendra Prasad K
Guest
Rajendra Prasad K

When we validate SpringConfig.xml, it is throwing errors by the validation, and it is stoping me to proceed further.

Madan
Guest
Madan
Gulshan
Guest
Gulshan

HTTP Status 500 – Servlet.init() for servlet dispatcher threw exception

type Exception report

message Servlet.init() for servlet dispatcher threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
java.lang.Thread.run(Thread.java:619)

root cause

java.lang.NoClassDefFoundError: com/mongodb/ReadPreference
java.lang.Class.getDeclaredMethods0(Native Method)
java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
java.lang.Class.getDeclaredMethod(Class.java:1935)
org.springframework.core.LocalVariableTableParameterNameDiscoverer$LocalVariableTableVisitor.resolveMember(LocalVariableTableParameterNameDiscoverer.java:236)
org.springframework.core.LocalVariableTableParameterNameDiscoverer$LocalVariableTableVisitor.visitEnd(LocalVariableTableParameterNameDiscoverer.java:219)
org.springframework.asm.ClassReader.accept(Unknown Source)
org.springframework.asm.ClassReader.accept(Unknown Source)
org.springframework.core.LocalVariableTableParameterNameDiscoverer.inspectClass(LocalVariableTableParameterNameDiscoverer.java:114)
org.springframework.core.LocalVariableTableParameterNameDiscoverer.getParameterNames(LocalVariableTableParameterNameDiscoverer.java:86)
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:193)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
java.lang.Thread.run(Thread.java:619)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.30 logs.

Apache Tomcat/7.0.30

trackback
spring data mongodb 1.0.2.RELEASE | Just do it.
Veer
Guest
Veer

Hello,

I am new to MongoDB.

I am getting Error -> The method getBean(String) is undefined for the type ApplicationContext. for the following

MongoOperations mongoOperation = (MongoOperations) ctx.getBean(“mongoTemplate”);

Please let me know which version of JAR do I need to use?

Greg
Guest
Greg

I get org/springframework/dao/InvalidDataAccessApiUsageException. Isn’t this a relic of Spring 2?

Maxim
Guest
Maxim

seems this is invalid:

there is no ‘mongoDbFactory’

JJ Zabkar
Guest
JJ Zabkar

FYI, the link in section 3 is broken (“hhttp”).

trackback
Java MongoDB Tutorial

[…] Spring Data MongoDB hello world example Configure (both XML and annotation) and perform CRUD operations with “Spring Data for MongoDB” framework. […]

trackback
Spring Data MongoDB : Save binary file, GridFS example

[…] Spring Data MongoDB Hello World Example […]

trackback
Spring Data MongoDB : Update document

[…] Spring Data MongoDB Hello World Example […]

trackback
Spring Data MongoDB : Insert document

[…] Spring Data MongoDB Hello World Example […]

Haris
Guest
Haris

Thank you for the sample,

It is simple and well understandable.

Srinivasa HP
Guest
Srinivasa HP

Pls let us know how to close the mongoDB connection.
FYI – we are using Spring Data to connect to mongoDB

Hans Herrera Bravo
Guest
Hans Herrera Bravo

persist data? with implementation DataNucleus?

Vincent
Guest
Vincent

Can you show us how to close the mongoDB connection in this example ?

Bob Marks
Guest
Bob Marks

The Spring XML config isn’t quite correct – its missing a “mongo-ref” attribute. For example, if your Mongo database is on another server. i.e.


It won’t connect (always defaults to localhost) so you have to update the


XML to look like the following: –


Nice example all the same!

trackback
spring data mongodb 1.0.2.RELEASE | Just do it.