Your SlideShare is downloading. ×
MongoDB & Spring MVC Integration
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

MongoDB & Spring MVC Integration


Published on

Introduction to NoSQL and MongoDB along with step by step walkthrough on how to integrate MongoDB with the Spring MVC framework. Full source code available for example project from …

Introduction to NoSQL and MongoDB along with step by step walkthrough on how to integrate MongoDB with the Spring MVC framework. Full source code available for example project from

Published in: Technology, Education

1 Comment
  • nice one. application development made easy, no pain of object to relation mapping.
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Hands-on NoSQL
    MongoDB & Spring Integration
  • 2.
    • I’m a professional Software Engineer based in the UK.
    • 3. Neither I, nor this presentation, are in any way affiliated with SpringSource or MongoDB
    • 4. Still want more?
  • 5. What is NoSQL?
    • NoSQL means “Not Only SQL”
    • 6. The NoSQL umbrella covers a range of non-relational databases, including key-value, document , wide column store and graph based databases
  • Why NoSQL?
    • NoSQL’s recent increase in popularity being largely driven by the explosion in amount of data that is now being created/captured on the web
    • 7. Many leading web 2.0 sites now use NoSQL implementations:
    Facebook – Cassandra (originally developed in-house and later open-sourced
    Twitter – Hadoop & Cassandra
    Google – BigTable
    Digg – Cassandra
    FourSquare - MongoDB
  • 8. In the News
    Percent increase in NoSQL jobs since November 2009 as reported by
    Amount raised in Series C Funding last week by CouchBase for further investment in to NoSQL solutions(Aug2011)
    Amount of data generated everyday in Twitter, and needs to be supported by their NoSQL solution (figure from 2010)
  • 9. MongoDB
    • An Open Source Document oriented database
    • 10. Dynamic schemas, storing data in JSON style documents that allows for addition of data on the fly
    • 11. Supports indexing of documents
    • 12. Auto-sharding supported, providing full horizontal scaling
  • MongoDB & Spring
    • Core Spring and the SpringMVC framework areenterprise standard Javalibraries for building professional web sites
    • 13. As more and more web sites need to be able to handle huge amounts of data, there will be an increased need to be able to build professional sites incorporating NoSQL solutions
  • How Spring & MongoDB Fit Together
    • Spring enables complete configuration of the Controller, Service, DAO Layer and facilitates connection to Mongo DB
    Servlet Engine (e.g. Tomcat)
    Controller Layer
    Service Layer
    DAO Layer
    Front Controller (Spring Dispatcher)
    Mongo DB
    Mongo Repository
  • 14. Getting Started
    • This overview does not cover details of setting up a Spring MVC app, and will assume a working knowledge of the framework
    • 15. Only the configuration to connect an existing Spring MVC app will be covered in detail
    • 16. Before proceeding, install MongoDB – full details are available:
    • 17. If you plan to follow the walkthrough with the source code, it is provided as an Eclipse/Maven project and uses the M2Eclipse plugin
  • Domain Objects
    • Similar to using JPA(Hibernate, etc) the first step is to model the underlying Domain objects you want to use to model the applications underlying meta-data.
    • 18. Using Spring Data we can model ourdomain using POJOsand annotations.
  • Domain Objects
    • @Document annotation on the root Document object
    • 19. @Id on any member variable to be used as an identifier for the object (must be String/int/long)
    • 20. The @Id field must either be named “id”, or the annotation used in combination with a MappingConverter
  • Domain Objects
    • Unlike traditional JPA models, Document based models will often have a root object, and will often contain nested objects, which do not need to be annotated
    • 21. In our Resume example, only our top level “Resume” object needs to be annotated and we will only be persisting/retrieving at this level (Document based DB makes sense for this model, as a “ResumePage” only makes sense within the context of a “Resume” and not as an object in itself)
    • 22. Using JPA, all three objects would be annotated “@Entity” and all could be queried as independent objects
    @Document public class Resume@Idprivate String id;
    public class ResumePage
    public class PageSection
  • 23. Data Access – The Repository
    • Core CRUD functionality can be achieved out-of-the-box by extending the “MongoRepository” class
    • 24. This provides save(), findAll(), findOne() and delete() methods that can be used on our @Document object (in our case “”)
  • Data Access – The Repository
    • Our Repository interface can easily be extended to include custom methods, and the MongoTemplate class provides an API to create custom queries:mongoTemplate.find(“COLLECTION_NAME”, new Query(Criteria.where(“name”).is(searchTerm)), Resume.class);
    • 25. Criteria allows querying by any field name in a MongoDB Document
  • Service Layer
    • The majority of the MongoDB implementation is abstracted to the Data Access layer through the MongoRepository
    • 26. There is no real change in the Service or Controller Layers to the normal Spring MVC approach
    • 27. In the Service class we can just auto-wire the Repository Interface in to the class just like we would normally with the DAOs
  • Configuration
    <!-- Mongo Configuration -->
    <mongo:repositories base-package="com.tmm.nosql.mongodb.repo" />
    <bean id="mongoTemplate" class="">
    <constructor-arg ref="mongoDbFactory" />
    <!-- local config -->
    <mongo:db-factory id="mongoDbFactory" dbname="resume_db“
    host="localhost" port="27017"/>
    1. Define package location for the Repository
    2. Define MongoTemplate bean
    3. Define the Mongo DB Factory with the host name, port, dbname (can also provide username and password if needed)
  • 28. Deploying to CloudFoundry
    • Sign up for your free CloudFoundry account (currently in Beta, so it may take time for your account details) at
    • 29. Install the latest STS Eclipse build and CloudFoundry extensions (full details here:
  • App Config for CloudFoundry
    • A change to your application config is needed to support access to CloudFoundry MongoDB service
    • 30. Notice the only change is the “mongoDbFactory” bean is being instantiated with no constructor arguments – this will all be injected by Cloud Foundry
  • Configuring CloudFoundry
    In the Servers tab, double-click the deployed application to open the Applications tab
    In the Services section of the Applications tab, select the “Add Service” button, select MongoDB and name the service appropriately
    Once the MongoDB service has been created, click and drag it in to the “Application Services” tab
  • 31. Thoughts
    • NoSQL is growing in popularity but is not intended to be a replacement for RDBMS
    • 32. NoSQL should be considered as another tool in the arsenal – RDBMS is not going anywhere!
    • 33. Database technology choice should be driven by the requirements of the problem – Document oriented have specific applications
  • Appendix
    • Full source code for the application is available from
    • 34. Feel free to drop me an email or come by to the blog and leave a comment if you have any questions or feedback