{"id":51243,"date":"2017-10-17T11:00:18","date_gmt":"2017-10-17T08:00:18","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=51243"},"modified":"2018-01-30T14:44:56","modified_gmt":"2018-01-30T12:44:56","slug":"java-mongodb-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/","title":{"rendered":"Java MongoDB Example"},"content":{"rendered":"<p><strong>MongoDb<\/strong> is the leading NoSQL database system which has become popular due to its dynamic schema nature and the advantages over the Big Data like high performance, horizontal scalability, replication etc. Unlike the traditional database systems which provide their own Jdbc-compliant drivers, MongoDb comes with its <span style=\"text-decoration: underline\">own in-Jdbc driver called Mongo Java Driver<\/span>. That means developers cannot use the standard Jdbc API to interact with the MongoDb from Java. In this tutorial, we will show how to implement the MongoDb in Java.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#whatIsMongoDb\">1.1 What is MongoDb?<\/a><\/dt>\n<dt><a href=\"#whatIsNoSql\">1.2 What is NoSQL?<\/a><\/dt>\n<dt><a href=\"#whyToUseMongoDb\">1.3 Why use MongoDb?<\/a><\/dt>\n<dt><a href=\"#advantagesOverRelationalDb\">1.4 Advantages Over RDBMS<\/a><\/dt>\n<dt><a href=\"#downloadAndInstallMongoDb\">1.5 Download and Install MongoDb<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#javaMongoDbExample\">2. Java MongoDb Example<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#toolsUsed\">2.1 Tools Used<\/a><\/dt>\n<dt><a href=\"#projectStructure\">2.2 Project Structure<\/a><\/dt>\n<dt><a href=\"#projectCreation\">2.3 Project Creation<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#applicationBuilding\">3. Application Building<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#mavenDependencies\">3.1 Maven Dependencies<\/a><\/dt>\n<dt><a href=\"#javaClassCreation\">3.2 Java Class Creation<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#runTheApplication\">4. Run the Application<\/a><\/dt>\n<dt><a href=\"#projectDemo\">5. Project Demo<\/a><\/dt>\n<dt><a href=\"#projectConclusion\">6. Conclusion<\/a><\/dt>\n<dt><a href=\"#projectDownload\">7. Download the Eclipse Project<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>MongoDb is a <span style=\"text-decoration: underline\">document-oriented NoSQL database<\/span> used for high volume data storage and provides high performance, high availability and automatic scaling.<\/p>\n<h3><a name=\"whatIsMongoDb\"><\/a>1.1 What is MongoDb?<\/h3>\n<ul>\n<li>MongoDB is a <em>NoSQL database<\/em> where each db contains collections which in turn contains documents. Each document has a different number of fields, size, and content. Each documentation in MongoDB is stored in a JSON-like format (i.e. <a href=\"https:\/\/en.wikipedia.org\/wiki\/BSON\" target=\"_blank\" rel=\"noopener\">Binary JSON (BSN<\/a>)<\/li>\n<li>The documents in MongoDB doesn\u2019t need to have a schema defined beforehand. Instead, the fields (i.e. <em>records<\/em>) can be created on the go<\/li>\n<li>Data model available within the MongoDB allows developers to represent hierarchical relationships, store arrays, and other more complex structures more easily<\/li>\n<li>This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability<\/li>\n<\/ul>\n<h3><a name=\"whatIsNoSql\"><\/a>1.2 What is NoSQL?<\/h3>\n<p>NoSQL Db is used to refer a non-SQL or the non-relational database concepts.<\/p>\n<ul>\n<li>Provides a mechanism for storage and retrieval of the data other than the tabular relational model used in the relational databases<\/li>\n<li>Offers flexibility since records are not restricted to the identical column names and types defined across the entire table<\/li>\n<li>Do not use the standard <code>SQL<\/code> language to query the data and provides <strong>no<\/strong> strict schema<\/li>\n<li>With NoSQL, <span style=\"text-decoration: underline\">ACID<\/span> (Atomicity, Consistency, Isolation, Durability) properties of the database transactions are not guaranteed<\/li>\n<li>Offers scalability and high-performance for handling the huge volumes of data<\/li>\n<\/ul>\n<h3><a name=\"whyToUseMongoDb\"><\/a>1.3 Why MongoDb?<\/h3>\n<ul>\n<li>As a NoSQL type database, MongoDb stores the data in the form of a document. Thus, MongoDb offers more flexibility<\/li>\n<li>This database supports search by field-name, range queries, and the regular expressions. It often provides queries to return the particular fields inside the documents<\/li>\n<li>MongoDb offers <em>Indexes<\/em> in order to improve the <em>search performance<\/em> within the NoSQL database<\/li>\n<li>To offer horizontal scalability, MongoDb uses sharding by splitting the data across the numerous MongoDb occurrences<\/li>\n<li><strong>Replication<\/strong>: MongoDb can give high availability with the replica sets<\/li>\n<\/ul>\n<h3><a name=\"advantagesOverRelationalDb\"><\/a>1.4 Advantages Over Relational Database<\/h3>\n<p>The table below describes the differences between the MongoDb and a Relational Database<\/p>\n<table>\n<tbody>\n<tr>\n<td>Relational Database (RDBMS)<\/td>\n<td>MongoDb<\/td>\n<td>Difference<\/td>\n<\/tr>\n<tr>\n<td>Table<\/td>\n<td>Collection<\/td>\n<td>A relational database table has rows and columns that are used to store the data. This construction is called Collection in MongoDb. A Collection has documents which has fields (in a key-value pair).<\/td>\n<\/tr>\n<tr>\n<td>Row<\/td>\n<td>Document<\/td>\n<td>The row in a relational database represents a set of related data in a table, while in MongoDb the same data is stored in the form of documents (i.e. in a Binary JSON format).<\/td>\n<\/tr>\n<tr>\n<td>Column<\/td>\n<td>Field<\/td>\n<td>The column in a relational database is a set of value of a particular type, while these are called as <em>Fields<\/em> in MongoDb.<\/td>\n<\/tr>\n<tr>\n<td>Joins<\/td>\n<td>Embedded documents<\/td>\n<td>Sometimes, the data is spread across multiple tables in a relational database and to show the entire data, a <code>SQL<\/code> Join is suggested. While in case of MongoDb, this same data will be stored in a single collection, but partitioned by the documents. Thus, there is no term as <em>Joins<\/em> in the NoSQL database.<\/td>\n<\/tr>\n<tr>\n<td>Primary Key<\/td>\n<td><code>_id<\/code><\/td>\n<td>In MongoDb, the primary key is automatically set to the <code>_id<\/code> field.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><a name=\"downloadAndInstallMongoDb\"><\/a>1.5 Download and Install MongoDb<\/h3>\n<p>You can watch <a href=\"https:\/\/www.youtube.com\/watch?v=1uFY60CESlM\" target=\"_blank\" rel=\"noopener\">this<\/a> video in order to download and install the MongoDb database on your Windows Operating system.<\/p>\n<p>Now, open up the Eclipse Ide and let\u2019s see how to access the MongoDb using the Java Driver and how to perform the common <span style=\"text-decoration: underline\">CRUD<\/span> (Create, Read, Update, and Delete) operations.<\/p>\n<h2><a name=\"javaMongoDbExample\"><\/a>2. Java MongoDb Example<\/h2>\n<p>Here is a step-by-step guide for the CRUD operations using the MongoDb Java driver.<\/p>\n<h3><a name=\"toolsUsed\"><\/a>2.1 Tools Used<\/h3>\n<p>We are using Eclipse Kepler SR2, JDK 8, MongoDb and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h3><a name=\"projectStructure\"><\/a>2.2 Project Structure<\/h3>\n<p>Firstly, let\u2019s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!<\/p>\n<p><figure id=\"attachment_51244\" aria-describedby=\"caption-attachment-51244\" style=\"width: 235px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-structure.jpg\"><img decoding=\"async\" class=\"wp-image-51244 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-structure.jpg\" alt=\"Fig. 1: Java MongoDb Application Project Structure\" width=\"235\" height=\"212\" \/><\/a><figcaption id=\"caption-attachment-51244\" class=\"wp-caption-text\">Fig. 1: Java MongoDb Application Project Structure<\/figcaption><\/figure><\/p>\n<h3><a name=\"projectCreation\"><\/a>2.3 Project Creation<\/h3>\n<p>This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to <code>File -&gt; New -&gt; Maven Project<\/code>.<\/p>\n<p><figure id=\"attachment_51245\" aria-describedby=\"caption-attachment-51245\" style=\"width: 652px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51245\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-1.jpg\" alt=\"Fig. 2: Create Maven Project\" width=\"652\" height=\"686\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-1.jpg 652w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-1-285x300.jpg 285w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/a><figcaption id=\"caption-attachment-51245\" class=\"wp-caption-text\">Fig. 2: Create Maven Project<\/figcaption><\/figure><\/p>\n<p>In the New Maven Project window, it will ask you to select a project location. By default, &#8216;<em>Use default workspace location<\/em>&#8216; will be selected. Select the &#8216;<em>Create a simple project (skip archetype selection)<\/em>&#8216; checkbox and just click on next button to proceed.<\/p>\n<p><figure id=\"attachment_51246\" aria-describedby=\"caption-attachment-51246\" style=\"width: 804px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51246\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-2.jpg\" alt=\"Fig. 3: Project Details\" width=\"804\" height=\"541\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-2.jpg 804w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-2-300x202.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-2-768x517.jpg 768w\" sizes=\"(max-width: 804px) 100vw, 804px\" \/><\/a><figcaption id=\"caption-attachment-51246\" class=\"wp-caption-text\">Fig. 3: Project Details<\/figcaption><\/figure><\/p>\n<p>It will ask you to &#8216;Enter the group and the artifact id for the project&#8217;. We will input the details as shown in the below image. The version number will be by default: <code>0.0.1-SNAPSHOT<\/code>.<\/p>\n<p><figure id=\"attachment_51247\" aria-describedby=\"caption-attachment-51247\" style=\"width: 599px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-3.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51247\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-3.jpg\" alt=\"Fig. 4: Archetype Parameters\" width=\"599\" height=\"544\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-3.jpg 599w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-3-300x272.jpg 300w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/a><figcaption id=\"caption-attachment-51247\" class=\"wp-caption-text\">Fig. 4: Archetype Parameters<\/figcaption><\/figure><\/p>\n<p>Click on finish and the creation of a maven project will be completed. If you observe, it has downloaded the maven dependencies and a <code>pom.xml<\/code> file will be created. It will have the following code:<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\t&lt;groupId&gt;JavaMongoDb&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;JavaMongoDb&lt;\/artifactId&gt;\r\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>We can start adding the dependencies that developers want like MongoDb Java Driver, Junit etc. Let\u2019s start building the application!<\/p>\n<h2><a name=\"applicationBuilding\"><\/a>3. Application Building<\/h2>\n<p>Below are the steps involved in developing this application.<\/p>\n<h3><a name=\"mavenDependencies\"><\/a>3.1 Maven Dependencies<\/h3>\n<p>In this example, we are using the most stable MongoDb Java Driver Version (i.e. <code>mongo-java-driver-3.5.0<\/code>) in order to make the database connectivity. The <strong>updated<\/strong> file will have the following code:<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\t&lt;groupId&gt;JavaMongoDb&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;JavaMongoDb&lt;\/artifactId&gt;\r\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n\t&lt;dependencies&gt;\r\n\t\t&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.mongodb\/mongo-java-driver --&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.mongodb&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;mongo-java-driver&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;3.5.0&lt;\/version&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t&lt;\/dependencies&gt;\r\n\t&lt;build&gt;\r\n\t\t&lt;finalName&gt;${project.artifactId}&lt;\/finalName&gt;\r\n\t&lt;\/build&gt;\r\n&lt;\/project&gt;<\/pre>\n<p>MongoDb Java driver is an all-in-one jar, which embeds the core driver and BSON. BSON, short for <em>Binary JSON<\/em>, is a binary-encoded serialization of the JSON-like documents.<\/p>\n<h3><a name=\"javaClassCreation\"><\/a>3.2 Java Class Creation<\/h3>\n<p>Let\u2019s create the required Java files. Right-click on <code>src\/main\/java<\/code> folder, <code>New -&gt; Package<\/code>.<\/p>\n<p><figure id=\"attachment_51248\" aria-describedby=\"caption-attachment-51248\" style=\"width: 656px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-4.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51248\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-4.jpg\" alt=\"Fig. 5: Java Package Creation\" width=\"656\" height=\"660\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-4.jpg 656w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-4-150x150.jpg 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-4-298x300.jpg 298w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-4-70x70.jpg 70w\" sizes=\"(max-width: 656px) 100vw, 656px\" \/><\/a><figcaption id=\"caption-attachment-51248\" class=\"wp-caption-text\">Fig. 5: Java Package Creation<\/figcaption><\/figure><\/p>\n<p>A new pop window will open where we will enter the package name as: <code>com.jcg.java.mongodb<\/code>.<\/p>\n<p><figure id=\"attachment_51249\" aria-describedby=\"caption-attachment-51249\" style=\"width: 517px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-5.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51249\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-5.jpg\" alt=\"Fig. 6: Java Package Name (com.jcg.java.mongodb)\" width=\"517\" height=\"423\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-5.jpg 517w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-5-300x245.jpg 300w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/a><figcaption id=\"caption-attachment-51249\" class=\"wp-caption-text\">Fig. 6: Java Package Name (com.jcg.java.mongodb)<\/figcaption><\/figure><\/p>\n<p>Once the package is created, we will need to create the implementation class. Right-click on the newly created package, <code>New -&gt; Class<\/code>.<\/p>\n<p><figure id=\"attachment_51250\" aria-describedby=\"caption-attachment-51250\" style=\"width: 672px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-6.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51250\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-6.jpg\" alt=\"Fig. 7: Java Class Creation\" width=\"672\" height=\"700\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-6.jpg 672w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-6-288x300.jpg 288w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/a><figcaption id=\"caption-attachment-51250\" class=\"wp-caption-text\">Fig. 7: Java Class Creation<\/figcaption><\/figure><\/p>\n<p>A new pop window will open and enter the file name as: <code>JavaMongoDbDemo<\/code>. The implementation class will be created inside the package: <code>com.jcg.java.mongodb<\/code>.<\/p>\n<p><figure id=\"attachment_51251\" aria-describedby=\"caption-attachment-51251\" style=\"width: 536px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-7.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51251\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-7.jpg\" alt=\"Fig. 8: Java Class (JavaMongoDbDemo.java)\" width=\"536\" height=\"628\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-7.jpg 536w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-guide-7-256x300.jpg 256w\" sizes=\"(max-width: 536px) 100vw, 536px\" \/><\/a><figcaption id=\"caption-attachment-51251\" class=\"wp-caption-text\">Fig. 8: Java Class (JavaMongoDbDemo.java)<\/figcaption><\/figure><\/p>\n<h4>3.2.1 Implementation of Utility Class<\/h4>\n<p>In this class, we will be establishing a connection with the db for performing the db operations by using the MongoDb Java Driver. We need to perform the following steps:<\/p>\n<ul>\n<li>Using the <code>com.mongodb.MongoClient<\/code> class instance to make a db connection. The <code>MongoClient<\/code> class is thread-safe which means only one instance is needed even if multiple threads are being used<\/li>\n<li>Retrieving the db and the collection (i.e. table) details<\/li>\n<li>Performing the CRUD (i.e. Create, Read, Update, and Delete) operations on the retrieved collection\u2019s documents (i.e. records)<\/li>\n<li>Finally calling the <code>close()<\/code> to release all the resources associated with the instance of <code>MongoClient<\/code>.<\/li>\n<\/ul>\n<p>Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline\"><em>JavaMongoDbDemo.java<\/em><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.jcg.java.mongodb;\r\n\r\nimport java.util.Date;\r\nimport java.util.LinkedList;\r\n\r\nimport com.mongodb.BasicDBObject;\r\nimport com.mongodb.DB;\r\nimport com.mongodb.DBCollection;\r\nimport com.mongodb.DBCursor;\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.MongoException;\r\n\r\npublic class JavaMongoDbDemo {\r\n\r\n\tstatic String array_names[] = {\"John\", \"Tim\", \"Brit\", \"Robin\", \"Smith\", \"Lora\", \"Jennifer\", \"Lyla\", \"Victor\", \"Adam\"};\r\n\tstatic String array_address[][] ={{\"US\", \"FL\", \" Miami\"}, {\"US\", \"FL\", \" Orlando\"}, {\"US\", \"CA\", \"San Diego\"}, {\"US\", \"FL\", \" Orlando\"}, {\"US\", \"FL\", \" Orlando\"}, \r\n\t\t{\"US\", \"NY\", \"New York\"}, {\"US\", \"NY\", \"Buffalo\"}, {\"US\", \"TX\", \" Houston\"}, {\"US\", \"CA\", \"San Diego\"}, {\"US\", \"TX\", \" Houston\"}};\r\n\r\n\t\/\/ This Helper Method Is Used To Build The Random Friend's Data\r\n\tprivate static String[] pickFriends() {\r\n\t\tint numberOfFriends = (int) (Math.random() * 10);\r\n\t\tLinkedList&lt;String&gt; friends = new LinkedList&lt;String&gt;();\r\n\t\tint random = 0;\r\n\t\twhile(friends.size() &lt; numberOfFriends) {\r\n\t\t\trandom = (int) (Math.random()*10);\r\n\t\t\tif(!friends.contains(array_names[random])) {\r\n\t\t\t\tfriends.add(array_names[random]);\r\n\t\t\t}\r\n\t\t}\r\n\t\tString arr[] = {};\r\n\t\treturn  friends.toArray(arr);\r\n\t}\r\n\r\n\t\/\/ This Helper Method Is Used To Build The Random Address\r\n\tprivate static String[] pickAddress() {\r\n\t\tint random = (int) (Math.random() * 10);\r\n\t\treturn array_address[random];\r\n\t}\r\n\r\n\t@SuppressWarnings(\"deprecation\")\r\n\tpublic static void main(String[] args) {\r\n\t\ttry {\r\n\t\t\t\/\/ Connecting To The MongoDb Server Listening On A Default Port (i.e. 27017).\r\n\t\t\tMongoClient mongoClntObj = new MongoClient(\"localhost\", 27017);\r\n\r\n\t\t\t\/\/ Get MongoDb Database. If The Database Doesn't Exists, MongoDb Will Automatically Create It For You\r\n\t\t\tDB dbObj = mongoClntObj.getDB(\"mongodbdemo\");\r\n\r\n\t\t\t\/\/ Get MongoDb Collection. If The Collection Doesn't Exists, MongoDb Will Automatically Create It For You\r\n\t\t\tDBCollection collectionObj = dbObj.getCollection(\"jcg\");\r\n\r\n\t\t\t\/**** INSERT OPERATION ****\/\r\n\t\t\t\/\/ Creating The MongoDb Documents To Store Key-Value Pair\r\n\t\t\tBasicDBObject documentObj;\r\n\t\t\tString address[];\r\n\t\t\tfor(int i = 0 ; i &lt; array_names.length ; i++) {\r\n\t\t\t\tdocumentObj = new BasicDBObject();\t\t\t\t\r\n\t\t\t\tdocumentObj.append(\"name\", array_names[i]); \t\t\t\r\n\t\t\t\tdocumentObj.append(\"age\", (int)(Math.random() * 60));\t\t\t\t\r\n\t\t\t\tdocumentObj.append(\"joined_date\", new Date());\t\t\t\t\r\n\t\t\t\tdocumentObj.append(\"friends\", pickFriends()); \r\n\t\t\t\taddress = pickAddress();\t\t\t\t\r\n\t\t\t\tdocumentObj.append(\"address\", new BasicDBObject(\"country\",address[0]).append(\"state\", address[1]).append(\"city\", address[2])); \r\n\t\t\t\tcollectionObj.insert(documentObj);\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ Get MongoDb Collections Count\r\n\t\t\tSystem.out.println(\"Total Number Of MongoDb Collection?=  \"+ collectionObj.getCount());\r\n\r\n\t\t\t\/**** READ OPERATION ****\/\r\n\t\t\t\/\/ ------------------------------------ Get All Documents ------------------------------------\r\n\t\t\tDBCursor cursorObj = collectionObj.find();\r\n\t\t\ttry {\r\n\t\t\t\twhile(cursorObj.hasNext()) {\r\n\t\t\t\t\tSystem.out.println(cursorObj.next());\r\n\t\t\t\t}\r\n\t\t\t} finally {\r\n\t\t\t\tcursorObj.close();\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ ------------------------------------ Get Documents By Query ------------------------------------\r\n\t\t\tBasicDBObject selectQuery = new BasicDBObject(\"age\", new BasicDBObject(\"$gt\", 40));\t\t\t \r\n\t\t\tcursorObj = collectionObj.find(selectQuery);\r\n\t\t\tSystem.out.println(\"\\nPersons With Age Greater Than 40 Years?= \"+ cursorObj.count());\r\n\r\n\t\t\t\/**** UPDATE OPERATION ****\/\r\n\t\t\t\/\/ Update Documents Found By The Query i.e. Update The Documents Having 'Age &gt; 40'\r\n\t\t\tBasicDBObject ageDocument = new BasicDBObject();\r\n\t\t\tageDocument.put(\"age\", 20);\r\n\r\n\t\t\tBasicDBObject updateObj = new BasicDBObject();\r\n\t\t\tupdateObj.put(\"$set\", ageDocument);\r\n\r\n\t\t\tcollectionObj.update(selectQuery, updateObj, false, true);\r\n\r\n\t\t\t\/\/ Find &amp; Display\r\n\t\t\tcursorObj = collectionObj.find(selectQuery);\r\n\t\t\tSystem.out.println(\"Persons With Age &gt; 40 After Update?= \"+ cursorObj.count());\r\n\r\n\t\t\t\/\/ ------------------------------------ Get All Documents Again ------------------------------------\r\n\t\t\tcursorObj = collectionObj.find();\r\n\t\t\ttry {\r\n\t\t\t\twhile(cursorObj.hasNext()) {\r\n\t\t\t\t\tSystem.out.println(cursorObj.next());\r\n\t\t\t\t}\r\n\t\t\t} finally {\r\n\t\t\t\tcursorObj.close();\r\n\t\t\t}\r\n\r\n\t\t\t\/**** DELETE OPERATION ****\/\r\n\t\t\t\/\/ Dropping Collection From The MongoDb Database\r\n\t\t\tif(dbObj.collectionExists(\"jcg\")) {\r\n\t\t\t\tcollectionObj.drop();\r\n\t\t\t}\r\n\t\t\tSystem.out.println(\"\\n Collection Successfully Dropped From The Database\");\r\n\r\n\t\t\t\/\/ Dropping The MongoDb Database\r\n\t\t\tdbObj.dropDatabase();\r\n\t\t\tSystem.out.println(\"\\n Database Successfully Dropped\");\r\n\r\n\t\t\t\/**** DONE ****\/\r\n\t\t\tmongoClntObj.close();\r\n\t\t\tSystem.out.println(\"\\n! Demo Completed !\");\r\n\t\t} catch (MongoException mongoExObj) {\r\n\t\t\tmongoExObj.printStackTrace();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>Do note, by default MongoDb server, is always running in a trusted mode and it doesn\u2019t require an authentication.<\/p>\n<h2><a name=\"runTheApplication\"><\/a>4. Run the Application<\/h2>\n<p>To run the application, Right click on the <code>JavaMongoDbDemo<\/code> class, <code>Run As -&gt; Java Application<\/code>. Developers can debug the example and see what happens after every step. Enjoy!<\/p>\n<p><figure id=\"attachment_51252\" aria-describedby=\"caption-attachment-51252\" style=\"width: 828px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-deploy-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51252\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-deploy-1.jpg\" alt=\"Fig. 9: Run Application\" width=\"828\" height=\"654\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-deploy-1.jpg 828w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-deploy-1-300x237.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-deploy-1-768x607.jpg 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/a><figcaption id=\"caption-attachment-51252\" class=\"wp-caption-text\">Fig. 9: Run Application<\/figcaption><\/figure><\/p>\n<h2><a name=\"projectDemo\"><\/a>5. Project Demo<\/h2>\n<p>The code shows the following status as output:<\/p>\n<p><figure id=\"attachment_51253\" aria-describedby=\"caption-attachment-51253\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-demo-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51253\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-demo-1.jpg\" alt=\"Fig. 10: Application Output\" width=\"849\" height=\"344\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-demo-1.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-demo-1-300x122.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mongodb-example-project-demo-1-768x311.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-51253\" class=\"wp-caption-text\">Fig. 10: Application Output<\/figcaption><\/figure><\/p>\n<p>That\u2019s all for this post. Happy Learning!!<\/p>\n<h2><a name=\"projectConclusion\"><\/a>6. Conclusion<\/h2>\n<p>That\u2019s all for getting the developers started with the MongoDB Java Driver. We will look into more features in the next posts. I hope this article served you whatever you were looking for and developers can download the sample application as an Eclipse project in the <a href=\"#projectDownload\">Downloads<\/a> section.<\/p>\n<h2><a name=\"projectDownload\"><\/a>7. Download the Eclipse Project<\/h2>\n<p>This was an example of MongoDb and Java.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/JavaMongoDb.zip\" target=\"_blank\" rel=\"noopener\"><strong>JavaMongoDb<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>MongoDb is the leading NoSQL database system which has become popular due to its dynamic schema nature and the advantages over the Big Data like high performance, horizontal scalability, replication etc. Unlike the traditional database systems which provide their own Jdbc-compliant drivers, MongoDb comes with its own in-Jdbc driver called Mongo Java Driver. That means &hellip;<\/p>\n","protected":false},"author":119,"featured_media":36154,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1447],"tags":[189,478,1194],"class_list":["post-51243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-core-java-2","tag-java","tag-mongodb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java MongoDB Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this tutorial, we will show how to implement the MongoDb in a Java application and perform the basic database operations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java MongoDB Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we will show how to implement the MongoDb in a Java application and perform the basic database operations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-17T08:00:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-30T12:44:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-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=\"Yatin\" \/>\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=\"Yatin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\"},\"headline\":\"Java MongoDB Example\",\"datePublished\":\"2017-10-17T08:00:18+00:00\",\"dateModified\":\"2018-01-30T12:44:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/\"},\"wordCount\":1410,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"keywords\":[\"core java\",\"Java\",\"MongoDb\"],\"articleSection\":[\"MongoDB\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/\",\"name\":\"Java MongoDB Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"datePublished\":\"2017-10-17T08:00:18+00:00\",\"dateModified\":\"2018-01-30T12:44:56+00:00\",\"description\":\"In this tutorial, we will show how to implement the MongoDb in a Java application and perform the basic database operations.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Software Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MongoDB\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/mongodb\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Java MongoDB Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\",\"name\":\"Yatin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"caption\":\"Yatin\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java MongoDB Example - Java Code Geeks","description":"In this tutorial, we will show how to implement the MongoDb in a Java application and perform the basic database operations.","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:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/","og_locale":"en_US","og_type":"article","og_title":"Java MongoDB Example - Java Code Geeks","og_description":"In this tutorial, we will show how to implement the MongoDb in a Java application and perform the basic database operations.","og_url":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2017-10-17T08:00:18+00:00","article_modified_time":"2018-01-30T12:44:56+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","type":"image\/jpeg"}],"author":"Yatin","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"Java MongoDB Example","datePublished":"2017-10-17T08:00:18+00:00","dateModified":"2018-01-30T12:44:56+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/"},"wordCount":1410,"commentCount":3,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","keywords":["core java","Java","MongoDb"],"articleSection":["MongoDB"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/","url":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/","name":"Java MongoDB Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","datePublished":"2017-10-17T08:00:18+00:00","dateModified":"2018-01-30T12:44:56+00:00","description":"In this tutorial, we will show how to implement the MongoDb in a Java application and perform the basic database operations.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/java-mongodb-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Software Development","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/"},{"@type":"ListItem","position":3,"name":"MongoDB","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/mongodb\/"},{"@type":"ListItem","position":4,"name":"Java MongoDB Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/51243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=51243"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/51243\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/36154"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=51243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=51243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=51243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}