{"id":74799,"date":"2018-03-28T10:00:17","date_gmt":"2018-03-28T07:00:17","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=74799"},"modified":"2023-12-11T10:39:38","modified_gmt":"2023-12-11T08:39:38","slug":"spring-data-mongodb-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html","title":{"rendered":"Spring Data MongoDB Tutorial"},"content":{"rendered":"<p>In today&#8217;s world it is very important to get the application up and running as soon as possible. The application should also be easy to develop and maintain.<\/p>\n<p>Spring is such framework which provides the ease of integration with a lot of different frameworks which makes it easy to develop an application using Spring. One such integration is integration of Spring with MongoDB.<\/p>\n<h2>1. Introduction<\/h2>\n<p>In this tutorial we will discuss about the combination of most famous java framework &#8220;Spring&#8221; and the most famous NoSQL database &#8220;MongoDB&#8221;. MongoDB is a document based NoSQL database which stores the data in JSON like structure.<\/p>\n<p>SpringData and MongoDB\u00a0integration is provided by Spring for easy integration of the two and to provide an ease for developers without being bothered about writing multiple queries for insertion, update and deletion.<\/p>\n<p>Below are some of the features that are provided by SpringData MongoDB project:<\/p>\n<ol>\n<li>SpringData allows the usage of\u00a0<code>@Configuration<\/code> class and XML based configuration both.<\/li>\n<li>Data Access exception hierarchy of Spring is used for translation of exception.<\/li>\n<li>Integrated mapping between Java&#8217;s POJO and MongoDB&#8217;s document.<\/li>\n<li><code>MongoTemplate<\/code> class that makes it easy to use common MongoDB operations.<\/li>\n<li>In Addition to<code> MongoTemplate<\/code>, <code>MongoReader<\/code> and <code>MongoWriter<\/code> classes for low-level of Mapping.<\/li>\n<\/ol>\n<p>The best way to understand any technology is by practicing it and we are going to do the same now.<\/p>\n<p>Let&#8217;s now do a simple program to understand Spring Data MongoDB in detail.<\/p>\n<h2>2. Technologies and Tools<\/h2>\n<p>Let us look at the technologies and tool used for building the program.<\/p>\n<ol>\n<li>Eclispe\u00a0Oxygen.2 Release (4.7.2)<\/li>\n<li>Java &#8211; version 9.0.4<\/li>\n<li>Gradle &#8211; 4.6<\/li>\n<li>MongoDB server &#8211; 3.6<\/li>\n<li>MongoCompass &#8211; 3.6<\/li>\n<li>SpringDataMongoDB &#8211; 2.0.5-RELEASE<\/li>\n<\/ol>\n<h2>3. Project Structure<\/h2>\n<p>Our project structure will look as shown below.<\/p>\n<p><figure id=\"attachment_74995\" aria-describedby=\"caption-attachment-74995\" style=\"width: 361px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/ProjectStructure_JCG.jpg\"><img decoding=\"async\" class=\"wp-image-74995 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/ProjectStructure_JCG.jpg\" alt=\"\" width=\"361\" height=\"421\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/ProjectStructure_JCG.jpg 361w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/ProjectStructure_JCG-257x300.jpg 257w\" sizes=\"(max-width: 361px) 100vw, 361px\" \/><\/a><figcaption id=\"caption-attachment-74995\" class=\"wp-caption-text\">Project Structure for SpringDataMongoDB<\/figcaption><\/figure><\/p>\n<p>Gradle project structure will have above shown project structure. In case of pom.xml the project structure will differ slightly.<\/p>\n<h2>4. The Program<\/h2>\n<p>As part of this program we will try to accomplish below mentioned objectives.<\/p>\n<ol>\n<li>Save object to MongoDB<\/li>\n<li>Update object in MongoDB<\/li>\n<li>Remove object from MongoDB<\/li>\n<li>Get all objects from MongoDB<\/li>\n<\/ol>\n<p>Let&#8217;s now understand all the components of the program. First of all we will start with the program dependencies and the jars that are needed for the program.<\/p>\n<h3>4.1 Gradle<\/h3>\n<p>We are using Gradle for build as part of the program. The <code>build.gradle<\/code> file will look as shown below.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>build.gradle<\/em><\/span><\/p>\n<pre class=\"brush:bash\">apply plugin: 'java'\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n   compile group: 'org.springframework.data', name: 'spring-data-mongodb', version: '2.0.5.RELEASE'\n   implementation 'com.google.guava:guava:23.0'\n   testImplementation 'junit:junit:4.12'\n}\n\n<\/pre>\n<p>In the above <code>build.gradle<\/code> file <code>apply plugin: 'java'<\/code> tells us the plugin that needs to be set. For us it is Java plugin.<br \/>\n<code>repositories{}<\/code> lets us know the repository from which the dependency should be pulled. We have chosen <code>mavenCentral<\/code> to pull the dependency jars. We can use <code>jcenter<\/code> also for pulling the respective dependency jars.<br \/>\n<code>dependencies {}<\/code> tag is used to provide necessary jar file details that should be pulled for the project.<\/p>\n<h3>4.2 Configuration for MongoDB<\/h3>\n<p>In order to use MongoDB configuration we need to implement\u00a0<code>AbstractMongoConfiguration<\/code> class. The <code>MongoConfig.java<\/code> class will look as shown below. We are using annotations here instead of xml. But, even XML can also be used for the purpose of setting the configuration.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>MongoConfig.java class implementation<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial.config;\n\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.data.mongodb.config.AbstractMongoConfiguration;\nimport com.mongodb.MongoClient;\n\n@Configuration\npublic class MongoConfig extends AbstractMongoConfiguration {\n\n\t@Override\n\tpublic String getDatabaseName() {\n\t\treturn \"local\";\n\t}\n\n\t@Override\n\t@Bean\n\tpublic MongoClient mongoClient() {\n\t\treturn new MongoClient(\"127.0.0.1\");\n\t}\n}\n<\/pre>\n<p>The <code>@Configuration<\/code> is used to define the class <code>MongoConfig.java<\/code> as the configuration class. <code>@Bean<\/code> defines the <code>MongoClient<\/code> bean.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h3>4.3 Model class<\/h3>\n<p>We will now look at the model class. We are using <code>student.java<\/code> as the model class which contains attributes for Student like Name and age. The <code>Student.java<\/code> model class is used for mapping POJO to the MongoDB collection.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Model class for Student<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial.model;\n\nimport org.springframework.data.annotation.Id;\nimport org.springframework.data.mongodb.core.mapping.Document;\n\n@Document(collection = \"students\")\npublic class Student {\n\tpublic Student(String studentName, int studentAge) {\n\t\tthis.studentName = studentName;\n\t\tthis.studentAge = studentAge;\n\t}\n\t@Id\n\tprivate String id;\n\tString studentName;\n\tint studentAge;\n\n        public String getStudentName() {\n\t\treturn studentName;\n\t}\n\n\tpublic void setStudentName(String studentName) {\n\t\tthis.studentName = studentName;\n\t}\n\n\tpublic int getStudentAge() {\n\t\treturn studentAge;\n\t}\n\n\tpublic void setStudentAge(int studentAge) {\n\t\tthis.studentAge = studentAge;\n\t}\n\n\t@Override\n    public String toString() {\n        return String.format(\n                \"Student[id=%s, studentName='%s', studentAge=\"+studentAge+\"]\",\n                id, studentName);\n    }    \n}\n<\/pre>\n<p><code>@Document<\/code>\u00a0 defines the document. The property <code>collection<\/code> defines the collection which will be used for mapping with the collection. All the attributes that are mentioned as part of the collection should be available in the POJO class. <code>@Id<\/code>\u00a0defines the id of the collection.<\/p>\n<h3>4.4 CRUD operations<\/h3>\n<p>To perform CRUD operations like saving data, updating data, removing data and getting the data from MongoDB we will use <code>MongoOperations<\/code>.<\/p>\n<p>Now let&#8217;s look at the <code>MongoDBPOperations.java<\/code> class. This class contains implementation for all the methods for CRUD operations.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>MongoDBPOperations class which will be used for performing CRUD operations<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial;\n\nimport java.util.List;\n\nimport org.springframework.data.mongodb.core.MongoOperations;\nimport org.springframework.data.mongodb.core.query.Criteria;\nimport org.springframework.data.mongodb.core.query.Query;\nimport org.springframework.data.mongodb.core.query.Update;\n\nimport com.tutorial.model.Student;\n\npublic class MongoDBPOperations {\n\npublic void saveStudent(MongoOperations mongoOperation, Student student) {\n\t\t\n\t\tmongoOperation.save(student);\n\t\tSystem.out.println(\"Student saved successfully\");\n\t\t\/\/ student object got created with id.\n\t\tSystem.out.println(\"student : \" + student);\n\t}\n\t\n\tpublic void searchStudent(MongoOperations mongoOperation, String critera,String value) {\n\t\t\/\/ query to search student\n\t\tQuery searchStudent = new Query(Criteria.where(critera).is(value));\n\n\t\t\/\/ find student based on the query\n\t\tStudent resultStudent = mongoOperation.findOne(searchStudent, Student.class);\n\t\tSystem.out.println(\"Student found!!\");\n\t\tSystem.out.println(\"Student details: \" + resultStudent);\n\t}\n\t\n\tpublic void updateStudent(MongoOperations mongoOperation, String critera,String value, String updateCriteria, String updateValue) {\n\t\t\/\/ query to search student\n\t\tQuery searchStudent = new Query(Criteria.where(critera).is(value));\n\t\tmongoOperation.updateFirst(searchStudent, Update.update(updateCriteria, updateValue),\n\t\t\t\tStudent.class);\n\t\tSystem.out.println(\"Student got updated successfully\");\n\t}\n\tpublic void getAllStudent(MongoOperations mongoOperation) {\n\t\tList listStudent = mongoOperation.findAll(Student.class);\n\t\tfor(Student student:listStudent) {\n\t\tSystem.out.println(\"Student = \" + student);\n\t\t}\n\t}\n\tpublic void removeStudent(MongoOperations mongoOperation, String critera,String value) {\n\t\tQuery searchStudent = new Query(Criteria.where(critera).is(value));\n\t\tmongoOperation.remove(searchStudent, Student.class);\n\t\tSystem.out.println(\"Student removed successfully!! \");\n\t}\n}\n<\/pre>\n<p>The most important class of a Java program is the class that contains the main method.<br \/>\n[ulp id=&#8217;c4qGwqz2zZ7CesZd&#8217;]<br \/>\n&nbsp;<\/p>\n<h3>4.5 Application class<\/h3>\n<p>The main class which contains the main method is the <code>Application.java<\/code> class. We will use this class to call methods from <code>MongoDBPOperations<\/code> class.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Application class for calling methods of MongoDBPOperations class<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial;\n\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.data.mongodb.core.MongoOperations;\nimport com.tutorial.config.MongoConfig;\nimport com.tutorial.model.Student;\n\n\npublic class Application {\n\t\n\tpublic static void main (String[] args) {\n\t\t\n    \t   \t\n\t\t\/\/ For Annotation\n\t\t\t\tApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class);\n\t\t\t\tMongoOperations mongoOperation = (MongoOperations) ctx.getBean(\"mongoTemplate\");\n\t\t\t\tMongoDBPOperations ops = new MongoDBPOperations();\n\n\t\t\t\tStudent student = new Student(\"John\", 15);\n\t\t\t\t\n\t\t\t\t\/\/save student\n\t\t\t\tops.saveStudent(mongoOperation, student);\n\n\t\t\t\t\/\/ get student based on search criteria\n\t\t\t\tops.searchStudent(mongoOperation, \"studentName\", \"John\");\n\t\t\t\t\n\t\t\t\t\/\/update student based on criteria\n\t\t\t\tops.updateStudent(mongoOperation, \"StudentName\", \"John\", \"studentAge\", \"18\");\n\t\t\t\t\/\/ get student based on search criteria\n\t\t\t\tops.searchStudent(mongoOperation, \"studentName\", \"John\");\n\t\t\t\t\n\t\t\t\t\/\/ get all the students\n\t\t\t\tops.getAllStudent(mongoOperation);\n\t\t\t\t\n\t\t\t\t\/\/remove student based on criteria\n\t\t\t\tops.removeStudent(mongoOperation, \"studentName\", \"John\");\n\t\t\t\t\/\/ get all the students\n\t\t\t\tops.getAllStudent(mongoOperation);\n\t\t\t\t\n\n    }\n\t\n\t\n\n}\n<\/pre>\n<p>Let us look a step by step operation that are performed in the <code>Application.java<\/code> class:<\/p>\n<ol>\n<li>We are creating <code>ApplicationContext<\/code>. This is due to the need of loading the configuration.<\/li>\n<li>In addition, <code>MongoOperations<\/code> object is created to load <code>MongoTemplate<\/code> bean.<\/li>\n<li><code>MongoDBOperations<\/code> object provides access to the methods to perform different <code>MongoOperation<\/code> methods.<\/li>\n<li>Furthermore Create a Student object with Name John and Age as 15.<\/li>\n<li>We are calling the <code>saveMethod<\/code> of\u00a0<code>MongoDBOperations<\/code> and we will pass the necessary parameters for saving the object in the Database.<\/li>\n<li>Similarly we are calling different methods of <code>MongoDBOperations<\/code> one by one.<\/li>\n<\/ol>\n<h3>4.6 Run the program<\/h3>\n<p>Finally let us now run the program as a java application. Right click on the Application.java -&gt; Run as -&gt; Java Application.<\/p>\n<p>Following result will appear on the console.<\/p>\n<p><figure id=\"attachment_75004\" aria-describedby=\"caption-attachment-75004\" style=\"width: 858px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_JCG.jpg\"><img decoding=\"async\" class=\"wp-image-75004 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_JCG.jpg\" alt=\"\" width=\"858\" height=\"219\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_JCG.jpg 858w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_JCG-300x77.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_JCG-768x196.jpg 768w\" sizes=\"(max-width: 858px) 100vw, 858px\" \/><\/a><figcaption id=\"caption-attachment-75004\" class=\"wp-caption-text\">Console output after running the program<\/figcaption><\/figure><\/p>\n<p>Now let us comment the command for removing object. MongoDB will store data successfully.<\/p>\n<p>Furthermore let us comment the line for removing the object as shown below.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Application class after commenting remove methods<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial;\n\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\nimport org.springframework.data.mongodb.core.MongoOperations;\nimport com.tutorial.config.MongoConfig;\nimport com.tutorial.model.Student;\n\n\npublic class Application {\n\t\n\tpublic static void main (String[] args) {\n\t\t\n    \t   \t\n\t\t\/\/ For Annotation\n\t\t\t\tApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class);\n\t\t\t\tMongoOperations mongoOperation = (MongoOperations) ctx.getBean(\"mongoTemplate\");\n\t\t\t\tMongoDBPOperations ops = new MongoDBPOperations();\n\n\t\t\t\tStudent student = new Student(\"John\", 15);\n\t\t\t\t\n\t\t\t\t\/\/save student\n\t\t\t\tops.saveStudent(mongoOperation, student);\n\n\t\t\t\t\/\/ get student based on search criteria\n\t\t\t\tops.searchStudent(mongoOperation, \"studentName\", \"John\");\n\t\t\t\t\n\t\t\t\t\/\/update student based on criteria\n\t\t\t\tops.updateStudent(mongoOperation, \"StudentName\", \"John\", \"studentAge\", \"18\");\n\t\t\t\t\/\/ get student based on search criteria\n\t\t\t\tops.searchStudent(mongoOperation, \"studentName\", \"John\");\n\t\t\t\t\n\t\t\t\t\/\/ get all the students\n\t\t\t\tops.getAllStudent(mongoOperation);\n\t\t\t\t\n\t\t\t\t\/\/remove student based on criteria\n\t\t\t\t\/\/ops.removeStudent(mongoOperation, \"studentName\", \"John\");\n\t\t\t\t\/\/ get all the students\n\t\t\t\t\/\/ops.getAllStudent(mongoOperation);\n\t\t\t\t\n\n    }\n\t\n\t\n\n}\n<\/pre>\n<p>As a result of change in the program let us re-run the program. The following will appear on the console.<\/p>\n<p><figure id=\"attachment_75005\" aria-describedby=\"caption-attachment-75005\" style=\"width: 798px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_No_remove_JCG.jpg\"><img decoding=\"async\" class=\"size-full wp-image-75005\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_No_remove_JCG.jpg\" alt=\"\" width=\"798\" height=\"209\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_No_remove_JCG.jpg 798w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_No_remove_JCG-300x79.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/Console_No_remove_JCG-768x201.jpg 768w\" sizes=\"(max-width: 798px) 100vw, 798px\" \/><\/a><figcaption id=\"caption-attachment-75005\" class=\"wp-caption-text\">Console when remove command is commented<\/figcaption><\/figure><\/p>\n<p>As a result of commenting the remove command MongoDB will store the data and hence will look as shown below.<\/p>\n<p><figure id=\"attachment_75206\" aria-describedby=\"caption-attachment-75206\" style=\"width: 806px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/MongoDB2_JCG.jpg\"><img decoding=\"async\" class=\"size-full wp-image-75206\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/MongoDB2_JCG.jpg\" alt=\"\" width=\"806\" height=\"322\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/MongoDB2_JCG.jpg 806w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/MongoDB2_JCG-300x120.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/MongoDB2_JCG-768x307.jpg 768w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/a><figcaption id=\"caption-attachment-75206\" class=\"wp-caption-text\">MongoDB Output after save and update command<\/figcaption><\/figure><\/p>\n<h2>5. Download the Eclipse project<\/h2>\n<p>This was an example of Spring Data MongoDB.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <strong><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/SpringDataMongoDbTutorial.zip\">SpringDataMongoDBTutorial.zip<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s world it is very important to get the application up and running as soon as possible. The application should also be easy to develop and maintain. Spring is such framework which provides the ease of integration with a lot of different frameworks which makes it easy to develop an application using Spring. One &hellip;<\/p>\n","protected":false},"author":22230,"featured_media":240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[112,321],"class_list":["post-74799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-mongodb","tag-spring-data"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Data MongoDB Tutorial - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In today&#039;s world it is very important to get the application up and running as soon as possible. The application should also be easy to develop and\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Data MongoDB Tutorial - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In today&#039;s world it is very important to get the application up and running as soon as possible. The application should also be easy to develop and\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-28T07:00:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-11T08:39:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Anand Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anand Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html\"},\"author\":{\"name\":\"Anand Kumar\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/69738684d820603e2d7199877fcab97e\"},\"headline\":\"Spring Data MongoDB Tutorial\",\"datePublished\":\"2018-03-28T07:00:17+00:00\",\"dateModified\":\"2023-12-11T08:39:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html\"},\"wordCount\":965,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"keywords\":[\"MongoDB\",\"Spring Data\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html\",\"name\":\"Spring Data MongoDB Tutorial - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"datePublished\":\"2018-03-28T07:00:17+00:00\",\"dateModified\":\"2023-12-11T08:39:38+00:00\",\"description\":\"In today's world it is very important to get the application up and running as soon as possible. The application should also be easy to develop and\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"spring-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-mongodb-tutorial.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Data MongoDB Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/69738684d820603e2d7199877fcab97e\",\"name\":\"Anand Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g\",\"caption\":\"Anand Kumar\"},\"description\":\"Anand has graduated from Kuvempu University, India in Electronics and Instrumentation department. He has over 8 years of IT experience and is mainly involved in design and programming for websites and server side logic using Java oriented technologies like spring-boot, spring-data, spring-security, Hibernate, etc. He has worked with start-up companies and small business setup to Large Multinational companies.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/anand-kumar-54b4a1b9\\\/ \"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/anand-kumar\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Data MongoDB Tutorial - Java Code Geeks","description":"In today's world it is very important to get the application up and running as soon as possible. The application should also be easy to develop and","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Spring Data MongoDB Tutorial - Java Code Geeks","og_description":"In today's world it is very important to get the application up and running as soon as possible. The application should also be easy to develop and","og_url":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-03-28T07:00:17+00:00","article_modified_time":"2023-12-11T08:39:38+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","type":"image\/jpeg"}],"author":"Anand Kumar","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Anand Kumar","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html"},"author":{"name":"Anand Kumar","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/69738684d820603e2d7199877fcab97e"},"headline":"Spring Data MongoDB Tutorial","datePublished":"2018-03-28T07:00:17+00:00","dateModified":"2023-12-11T08:39:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html"},"wordCount":965,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","keywords":["MongoDB","Spring Data"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html","name":"Spring Data MongoDB Tutorial - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","datePublished":"2018-03-28T07:00:17+00:00","dateModified":"2023-12-11T08:39:38+00:00","description":"In today's world it is very important to get the application up and running as soon as possible. The application should also be easy to develop and","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","width":150,"height":150,"caption":"spring-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-mongodb-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Data MongoDB Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/69738684d820603e2d7199877fcab97e","name":"Anand Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g","caption":"Anand Kumar"},"description":"Anand has graduated from Kuvempu University, India in Electronics and Instrumentation department. He has over 8 years of IT experience and is mainly involved in design and programming for websites and server side logic using Java oriented technologies like spring-boot, spring-data, spring-security, Hibernate, etc. He has worked with start-up companies and small business setup to Large Multinational companies.","sameAs":["https:\/\/www.linkedin.com\/in\/anand-kumar-54b4a1b9\/ "],"url":"https:\/\/www.javacodegeeks.com\/author\/anand-kumar"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/74799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/22230"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=74799"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/74799\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=74799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=74799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=74799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}