Spring Data MongoDB Examples

In this tutorial we will look about some examples of Spring Data – MongoDB integration. How to Spring configure with NoSQL database and what the advantages of this we will explain in this chapter.

Getting started with MongoDB and Spring Data
The primary goal of the Spring Data project is to make it easier for developers to work with (No)SQL databases. The Spring Data project already has support for a number of  NoSQL type of databases.

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. The Spring Data MongoDB project provides integration with the MongoDB document database. Key functional areas of Spring Data MongoDB are a POJO centric model for interacting with a MongoDB DBCollection and easily writing a Repository style data access layer.

Spring Data

Spring Data makes it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services as well as provide improved support for relational database technologies.
Spring Data is an umbrella open source project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies.


  • Spring configuration support using Java based @Configuration classes or an XML namespace for a Mongo driver instance and replica sets.
  • MongoTemplate helper class that increases productivity performing common Mongo operations. Includes integrated object mapping between documents and POJOs.
  • Exception translation into Spring’s portable Data Access Exception hierarchy
  • Feature Rich Object Mapping integrated with Spring’s Conversion Service
  • Annotation based mapping metadata but extensible to support other metadata formats
  • Persistence and mapping lifecycle events
  • Low-level mapping using MongoReader/MongoWriter abstractions
  • Java based Query, Criteria, and Update DSLs
  • Automatic implementation of Repository interfaces including support for custom finder methods.
  • QueryDSL integration to support type-safe queries.
  • Cross-store persistance – support for JPA Entities with fields transparently persisted/retrieved using MongoDB
  • Log4j log appender
  • GeoSpatial integration
  • Map-Reduce integration
  • JMX administration and monitoring
  • CDI support for repositories
  • GridFS support

There are below the list of Spring Data and MongoDB examples. Spring Data for MongoDB examples to perform insert, update, query and delete documents from MongoDB.

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

2. Spring Data MongoDB : Insert document
Example to use Spring data save(), insert() and insertList() to save domain object into mongoDB database.

3. Spring Data MongoDB : Update document
Example to use Spring data save(), updateFirst() and updateMulti() to update existing domain object from mongoDB database.

4. Spring Data MongoDB : Query document
Example to use Spring data findOne(), find() and getCollection() to get / query documents from mongoDB.

5. Spring Data MongoDB : Delete document
Example to remove() and findAndRemove() to delete document from mongoDB.


  1. MongoDB official site
  2. Java MongoDB official tutorials
  3. Java development with MongoDB
  4. Spring data for MongoDB
  5. Spring data for MongoDB documentation