Spring has the capability to create loose coupling application's layers. To dealing with database we create a DAO layer. In Spring DAO layer contains the Repository class this class is responsible for all database related operations. In this example we will use the @Repository annotaion to lable the Repository class.
MongoDB is different kind of database, it is scalable, NoSQL Document Oriented database. MongoDB holds data in the form of document and collections. If we compare the MongoDb to other Relational databases then we can say that the document in the MongoDB is like a row in Relational database and collection is like a table which holds the collection of documents.
Document is like a key-value information. Document holds the all information of a single entity like a row in relational database. Key in the documents are called field.
Collection is a group of documents. It is like a table in relational database.
Spring provides the MongoTemplate class to handle the database related operations. This class will provides the important functions like save(), insert(), findAll(), remove
() etc. which facilitate the developer to use the MongoDB without any problem. The following sample code describes some of the functions.
Example of Spring MVC and MongoDB :
Use following dependencies in pom.xml