Spring Data MongoDB - Interview Questions

What support does Spring Framework provide for MongoDB?


Spring Framework supports MongoDb as a part of the overall Spring Data Framework. Following are some of the support features for MongoDB

Provides Spring configuration support, both java-based as well as XML based, for MongoDB driver and MongoDB replica sets.

Provides a helper class - MongoTemplate, that helps with common MongoDB operations, and object mapping between documents and POJOs.

Translates MongoDB exceptions to Spring Data framework's Data Access Exception hierarchy.

Provides rich annotation-based Object mapping features.

Provides Java-based Query, Criteria and Update DSLs.

How do you connect to MongoDB using Spring Framework?


You connect to MongoDB using Spring framework by instantiating an instance of com.mongodb.client.MongoClient. This can be done either by using java-based configuration as well as by using XML-based configuration.

public class AppConfig {
 public @Bean MongoClient mongoClient() {
  return MongoClients.create('mongodb://localhost:27017');

What are the key features of the MongoTemplate class provided by Spring Data Framework?


The MongoTemplate class is the key class provided by Spring Data MongoDB framework. It provides rich features to interact with MongoDb database. Following are some of the key features.

1. MongoTemplate class provides convenience operations to create, update, delete and query MongoDb databases. MongoTemplate class implements MongoOperations interface which provides methods similar to native MongoDB collections method names, such as - find(), findOne(), findAll(), findAndReplace(), insert(), save(), update() etc.

2. MongoTemplate class implements interface MongoConverter which facilitates mapping between domain objects and MongoDB documents.

3. MongoTemplate class translates MongoDb exceptions thrown by MongoDB driver to Spring Data Framework's Data Access Exception hierarchy.

How do you instantiate MongoTemplate?


MongoTemplate has multiple overloaded constructors that can be used to create an instance of MongoTemplate.

MongoTemplate(MongoClient mongoClient, String databaseName) - Takes MongoClient object and default database name.

MongoTemplate(MongoDatabaseFactory mongoFactory) - Takes a MongoDataBaseFactory object that encapsulates MongoClient, database, and its credentials.

How do you create, update and delete MongoDB documents using Spring Data MongoDB framework?


You can create, update and delete MongoDB documents using MongoTemplate, which provides convenience methods similar to native MongoDB collection methods.

insert(), update(), and remove() methods can be used to create, update and delete documents respectively.

MongoTemplate mongoTemplate = new MongoTemplate(MongoClients.create(), 'database');
//Create MongoDB document
Website w = new Website('interviewgrid','www.interviewgrid.com');
//find document
w = findById('interviewgrid',Website.class);
//update document
mongoTemplate.updateFirst(query(where('id')is('interviewgrid')),update('url','http://www.interviewgrid.com'), Website.class);
//delete document

What support does Spring Data JDBC framework provide?


Spring Data JDBC, part of Spring Data, builds on top of the core repository support of Spring Data and provides support for JDBC repositories.

Spring Data JDBC aims at being a simple, limited, opinionated ORM. It does not implement many features of JPA such as caching and lazy loading.

What are the key features provided by Spring Data JDBC?


Following are some key features provided by Spring Data JDBC.

CRUD operations with customizable NamingStrategy

Support for @Query operations

JavaConfig based repository configuration using @EnableJdbcRepositories.

Subscribe to our Questions


Spring - Interview Questions

Spring FrameworkSpring DataSpring Data JDBCSpring Data MongoDBSpring Web FlowSpring WebFluxSpring RESTful ServicesSpring Security
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example