{"id":8421,"date":"2018-04-18T04:18:20","date_gmt":"2018-04-17T21:18:20","guid":{"rendered":"https:\/\/huongdanjava.com\/?p=8421"},"modified":"2018-05-10T05:07:37","modified_gmt":"2018-05-09T22:07:37","slug":"manipulate-with-mongodb-in-java-application","status":"publish","type":"post","link":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html","title":{"rendered":"Manipulate with MongoDB in Java application"},"content":{"rendered":"<p>MongoDB is a NoSQL database that allows us to store complex, unstructured data types. In this tutorial, I will walk you through the basic steps to work with MongoDB in your Java applications.<\/p>\n<p>First, I will create a new Maven project as an example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8424 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-1.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"696\" height=\"482\" \/><\/p>\n<p>Similar to other database systems, in order to work with Java, we need to declare to use the MongoDB driver in our application.<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;dependency&gt;\r\n    &lt;groupId&gt;org.mongodb&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;mongodb-driver&lt;\/artifactId&gt;\r\n    &lt;version&gt;3.6.3&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>OK, let&#8217;s get started.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7304065639390615\" data-ad-slot=\"6822390817\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nTo connect to MongoDB, we will use the MongoClient object from its Driver library with the following declaration:<\/p>\n<pre class=\"lang:java decode:true\">MongoClientURI uri = new MongoClientURI(\"mongodb:\/\/localhost:27017\");\r\nMongoClient client = new MongoClient(uri);<\/pre>\n<p>Where the MongoClientURI object is the object that will manage the MongoDB information including the IP address and port that MongoDB is running. The MongoClient object will be the object responsible for connecting to MongoDB.<\/p>\n<p>In case you are using MongoDB server with authentication mode, then you need to declare username and password are assigned to the database that you are going to use.<\/p>\n<pre class=\"lang:java decode:true\">MongoCredential credential = MongoCredential.createCredential(\"khanh\", \"mongodb\", \"abc123\".toCharArray());\r\nList&lt;MongoCredential&gt; auths = new ArrayList&lt;MongoCredential&gt;();\r\nauths.add(credential);<\/pre>\n<p>Then, connect to MongoDB with the following code:<\/p>\n<pre class=\"lang:java decode:true\">ServerAddress serverAddress = new ServerAddress(\"localhost\", 27017);\r\nMongoClient client = new MongoClient(serverAddress, auths);<\/pre>\n<p>I will use MongoDB server without authentication mode in this tutorial.<\/p>\n<p>By default, MongoDB will run on port 27017 and if you are installing MongoDB on your\u00a0local machine then we do not need to declare the MongoClientURI object. You simply declare the following:<\/p>\n<pre class=\"lang:java decode:true \">MongoClient client = new MongoClient();<\/pre>\n<p>I&#8217;m using MongoDB on my machine so I just declare it like that!<\/p>\n<p>At this point, if you run the application:<\/p>\n<pre class=\"lang:java decode:true\">package com.huongdanjava.mongodb;\r\n\r\nimport com.mongodb.MongoClient;\r\n\r\npublic class Application {\r\n\r\n    public static void main(String[] args) {\r\n        MongoClient client = new MongoClient();\r\n    }\r\n}<\/pre>\n<p>You will see the following results:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8425 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-2.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"700\" height=\"419\" \/><\/p>\n<p>Look at the log, you can see that our application is connected to MongoDB.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7304065639390615\" data-ad-slot=\"6822390817\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>Once connected to MongoDB, now you can work with it.<\/p>\n<p><strong>First, you can get a list of existing databases in your current MongoDB using the listDatabaseNames() method.<\/strong><\/p>\n<p>For example:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.mongodb;\r\n\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.client.MongoCursor;\r\nimport com.mongodb.client.MongoIterable;\r\n\r\npublic class Application {\r\n\r\n    public static void main(String[] args) {\r\n        MongoClient client = new MongoClient();\r\n\r\n        MongoIterable&lt;String&gt; databaseNames = client.listDatabaseNames();\r\n        MongoCursor&lt;String&gt; iterator = databaseNames.iterator();\r\n        while (iterator.hasNext()) {\r\n            System.out.println(iterator.next());\r\n        }\r\n    }\r\n}<\/pre>\n<p>Result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8430 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-7.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"700\" height=\"724\" \/> <script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7304065639390615\" data-ad-slot=\"6822390817\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>We can select the database to use the getDatabase() method of the MongoClient object.<\/strong><\/p>\n<p>For example, if I have a database called mongodb in MongoDB, my code will look like this:<\/p>\n<pre class=\"lang:java decode:true \">MongoDatabase mongodb = client.getDatabase(\"mongodb\");<\/pre>\n<p>Just like when using the command line with MongoDB, if the database we choose to manipulate does not exist, this database will be automatically created.<\/p>\n<p>MongoDatabase object will hold all information about the Collections in the database that we are using. Therefore, from this MongoDatabase object, you can get the names of all Collections using the listCollectionNames() method.<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.mongodb;\r\n\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.client.MongoCursor;\r\nimport com.mongodb.client.MongoDatabase;\r\nimport com.mongodb.client.MongoIterable;\r\n\r\npublic class Application {\r\n\r\n    public static void main(String[] args) {\r\n        MongoClient client = new MongoClient();\r\n\r\n        MongoDatabase mongodb = client.getDatabase(\"mongodb\");\r\n\r\n        MongoIterable&lt;String&gt; collectionNames = mongodb.listCollectionNames();\r\n        MongoCursor&lt;String&gt; iterator = collectionNames.iterator();\r\n        while (iterator.hasNext()) {\r\n            System.out.println(iterator.next());\r\n        }\r\n    }\r\n}<\/pre>\n<p>My database &#8220;mongodb&#8221; is having the following Collections:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8426 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-3.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"700\" height=\"481\" \/><\/p>\n<p>The result would be:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8427 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-4.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"700\" height=\"731\" \/><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7304065639390615\" data-ad-slot=\"6822390817\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>To work with a Collection, you can use the getCollection() method of the MongoDatabase object to get the MongoCollection object for the Collection.<\/strong><\/p>\n<p>My example is as follows:<\/p>\n<pre class=\"lang:java decode:true \">MongoCollection&lt;Document&gt; student = mongodb.getCollection(\"student\");<\/pre>\n<p><strong>If you want to insert a Document into the Collection you can use the <\/strong>insertOne<strong>() method to insert a Document or <\/strong>insertMany<strong>() to insert multiple Documents at the same time.<\/strong><\/p>\n<p>A Document is a JSON string and we will use the Document object of the MongoDB Driver API to build it. For example, now I want to build a Document of student information including name and age, I will create Document object as follows:<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.mongodb;\r\n\r\nimport org.bson.Document;\r\n\r\npublic class Student extends Document {\r\n\r\n    private static final String NAME = \"name\";\r\n    private static final String AGE = \"age\";\r\n    public static final String COLLECTION_NAME = \"student\";\r\n\r\n    public String getName() {\r\n        return getString(NAME);\r\n    }\r\n\r\n    public void setName(String name) {\r\n        put(NAME, name);\r\n    }\r\n\r\n    public Integer getAge() {\r\n        return getInteger(AGE);\r\n    }\r\n\r\n    public void setAge(Integer age) {\r\n        put(AGE, age);\r\n    }\r\n}\r\n<\/pre>\n<p>then write code to insert a new Document:<\/p>\n<pre class=\"lang:java decode:true\">package com.huongdanjava.mongodb;\r\n\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.client.MongoCollection;\r\nimport com.mongodb.client.MongoDatabase;\r\nimport org.bson.Document;\r\n\r\npublic class Application {\r\n\r\n    public static void main(String[] args) {\r\n        MongoClient client = new MongoClient();\r\n\r\n        MongoDatabase mongodb = client.getDatabase(\"mongodb\");\r\n\r\n        MongoCollection&lt;Document&gt; studentCollection = mongodb.getCollection(Student.COLLECTION_NAME);\r\n\r\n        Student student = new Student();\r\n        student.setName(\"Khanh\");\r\n        student.setAge(31);\r\n\r\n        studentCollection.insertOne(student);\r\n    }\r\n}<\/pre>\n<p>Result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8428 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-5.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"700\" height=\"479\" \/><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7304065639390615\" data-ad-slot=\"6822390817\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Of course, you can also check the result from the code by using the find() method of the MongoCollection object.<\/strong><\/p>\n<p>This find() method has a Document object argument, which is the criteria for finding all the Documents in the current Collection with similar content.<\/p>\n<pre class=\"lang:java decode:true \">package com.huongdanjava.mongodb;\r\n\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.client.FindIterable;\r\nimport com.mongodb.client.MongoCollection;\r\nimport com.mongodb.client.MongoCursor;\r\nimport com.mongodb.client.MongoDatabase;\r\nimport org.bson.Document;\r\n\r\npublic class Application {\r\n\r\n    public static void main(String[] args) {\r\n        MongoClient client = new MongoClient();\r\n\r\n        MongoDatabase mongodb = client.getDatabase(\"mongodb\");\r\n\r\n        MongoCollection&lt;Document&gt; studentCollection = mongodb.getCollection(Student.COLLECTION_NAME);\r\n\r\n        Student student = new Student();\r\n        student.setName(\"Khanh\");\r\n        student.setAge(31);\r\n\r\n        FindIterable&lt;Document&gt; documents = studentCollection.find(student);\r\n        MongoCursor&lt;Document&gt; iterator = documents.iterator();\r\n        while (iterator.hasNext()) {\r\n            Document next = iterator.next();\r\n            System.out.println(next);\r\n        }\r\n    }\r\n}<\/pre>\n<p>In the example above, I looked for the Student Document in terms of name and age. You can expand the search results by one of these two criteria also.<\/p>\n<p>Result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8429 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-6.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"700\" height=\"638\" \/><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7304065639390615\" data-ad-slot=\"6822390817\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>You can also update one or more Documents at the same time.<\/strong><\/p>\n<p>We will use the updateOne() or updateMany() methods of the MongoCollection object to do this.<\/p>\n<p>There are many methods of overloading these two methods with different parameters. But basically, there are two main parameters, the Document object to find the Document need to update, the second Document object contains the content to update.<\/p>\n<p>In my example, suppose I need to update the age of students named Khanh to 32, then I will write the code as follows:<\/p>\n<pre class=\"lang:java decode:true\">package com.huongdanjava.mongodb;\r\n\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.client.*;\r\nimport org.bson.Document;\r\n\r\npublic class Application {\r\n\r\n    public static void main(String[] args) {\r\n        MongoClient client = new MongoClient();\r\n\r\n        MongoDatabase mongodb = client.getDatabase(\"mongodb\");\r\n\r\n        MongoCollection&lt;Document&gt; studentCollection = mongodb.getCollection(Student.COLLECTION_NAME);\r\n\r\n        Student studentFilter = new Student();\r\n        studentFilter.setName(\"Khanh\");\r\n\r\n        Student student = new Student();\r\n        student.setAge(32);\r\n        Document updateObject = new Document();\r\n        updateObject.put(\"$set\", student);\r\n\r\n        studentCollection.updateOne(studentFilter, updateObject);\r\n    }\r\n}<\/pre>\n<p>As you can see, the Document object containing the content to be updated needs to be built with the keyword &#8220;$ set&#8221; and its value is the information the student needs to update.<\/p>\n<p>Result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8431 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-8.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"700\" height=\"465\" \/><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-7304065639390615\" data-ad-slot=\"6822390817\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>And you can also delete one or more documents at once using the deleteOne() and deleteMany() methods.<\/strong><\/p>\n<p>For example:<\/p>\n<pre class=\"lang:java decode:true\">package com.huongdanjava.mongodb;\r\n\r\nimport com.mongodb.MongoClient;\r\nimport com.mongodb.client.*;\r\nimport com.mongodb.client.result.DeleteResult;\r\nimport org.bson.Document;\r\n\r\npublic class Application {\r\n\r\n    public static void main(String[] args) {\r\n        MongoClient client = new MongoClient();\r\n\r\n        MongoDatabase mongodb = client.getDatabase(\"mongodb\");\r\n\r\n        MongoCollection&lt;Document&gt; studentCollection = mongodb.getCollection(Student.COLLECTION_NAME);\r\n\r\n        Student student = new Student();\r\n        student.setName(\"Khanh\");\r\n\r\n        DeleteResult deleteResult = studentCollection.deleteOne(student);\r\n        System.out.println(deleteResult.getDeletedCount());\r\n    }\r\n}<\/pre>\n<p>Result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8432 size-full\" src=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2018\/04\/manipulate-with-mongodb-in-java-application-9.png\" alt=\"Manipulate with MongoDB in Java application\" width=\"700\" height=\"618\" \/><\/p>\n<p>&nbsp;<\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-right kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;right&quot;,&quot;id&quot;:&quot;8421&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;4&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Manipulate with MongoDB in Java application&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 4px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\"><\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB is a NoSQL database that allows us to store complex, unstructured data types. In this tutorial, I will walk you through the basic steps to work with MongoDB in your Java applications. First, I will create a new Maven project as an example: Similar&hellip; <a href=\"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":1327,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-8421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb-en","clearfix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Manipulate with MongoDB in Java application - Huong Dan Java<\/title>\n<meta name=\"description\" content=\"In this tutorial, I will guide you all how to manipulate with MongoDB in Java application.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Manipulate with MongoDB in Java application - Huong Dan Java\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, I will guide you all how to manipulate with MongoDB in Java application.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html\" \/>\n<meta property=\"og:site_name\" content=\"Huong Dan Java\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/nhkhanh2406\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/nhkhanh2406\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-17T21:18:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-05-09T22:07:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/08\/mongodb.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Khanh Nguyen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/KhanhNguyenJ\" \/>\n<meta name=\"twitter:site\" content=\"@KhanhNguyenJ\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khanh Nguyen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html\"},\"author\":{\"name\":\"Khanh Nguyen\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"headline\":\"Manipulate with MongoDB in Java application\",\"datePublished\":\"2018-04-17T21:18:20+00:00\",\"dateModified\":\"2018-05-09T22:07:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html\"},\"wordCount\":709,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"image\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2016\\\/08\\\/mongodb.png\",\"articleSection\":[\"MongoDB\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html\",\"name\":\"Manipulate with MongoDB in Java application - Huong Dan Java\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2016\\\/08\\\/mongodb.png\",\"datePublished\":\"2018-04-17T21:18:20+00:00\",\"dateModified\":\"2018-05-09T22:07:37+00:00\",\"description\":\"In this tutorial, I will guide you all how to manipulate with MongoDB in Java application.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html#primaryimage\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2016\\\/08\\\/mongodb.png\",\"contentUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2016\\\/08\\\/mongodb.png\",\"width\":400,\"height\":400},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/manipulate-with-mongodb-in-java-application.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/huongdanjava.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Manipulate with MongoDB in Java application\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#website\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/\",\"name\":\"Huong Dan Java\",\"description\":\"Java development tutorials\",\"publisher\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/huongdanjava.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/#\\\/schema\\\/person\\\/dc859d7f8cbea3b593e6738de9cbb82d\",\"name\":\"Khanh Nguyen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"url\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"contentUrl\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\",\"width\":1267,\"height\":1517,\"caption\":\"Khanh Nguyen\"},\"logo\":{\"@id\":\"https:\\\/\\\/huongdanjava.com\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg\"},\"description\":\"I love Java and everything related to Java.\",\"sameAs\":[\"https:\\\/\\\/huongdanjava.com\",\"https:\\\/\\\/www.facebook.com\\\/nhkhanh2406\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/KhanhNguyenJ\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Manipulate with MongoDB in Java application - Huong Dan Java","description":"In this tutorial, I will guide you all how to manipulate with MongoDB in Java application.","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:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html","og_locale":"en_US","og_type":"article","og_title":"Manipulate with MongoDB in Java application - Huong Dan Java","og_description":"In this tutorial, I will guide you all how to manipulate with MongoDB in Java application.","og_url":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html","og_site_name":"Huong Dan Java","article_publisher":"https:\/\/www.facebook.com\/nhkhanh2406","article_author":"https:\/\/www.facebook.com\/nhkhanh2406","article_published_time":"2018-04-17T21:18:20+00:00","article_modified_time":"2018-05-09T22:07:37+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/08\/mongodb.png","type":"image\/png"}],"author":"Khanh Nguyen","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/KhanhNguyenJ","twitter_site":"@KhanhNguyenJ","twitter_misc":{"Written by":"Khanh Nguyen","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html#article","isPartOf":{"@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html"},"author":{"name":"Khanh Nguyen","@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"headline":"Manipulate with MongoDB in Java application","datePublished":"2018-04-17T21:18:20+00:00","dateModified":"2018-05-09T22:07:37+00:00","mainEntityOfPage":{"@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html"},"wordCount":709,"commentCount":0,"publisher":{"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"image":{"@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html#primaryimage"},"thumbnailUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/08\/mongodb.png","articleSection":["MongoDB"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html","url":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html","name":"Manipulate with MongoDB in Java application - Huong Dan Java","isPartOf":{"@id":"https:\/\/huongdanjava.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html#primaryimage"},"image":{"@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html#primaryimage"},"thumbnailUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/08\/mongodb.png","datePublished":"2018-04-17T21:18:20+00:00","dateModified":"2018-05-09T22:07:37+00:00","description":"In this tutorial, I will guide you all how to manipulate with MongoDB in Java application.","breadcrumb":{"@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html#primaryimage","url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/08\/mongodb.png","contentUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2016\/08\/mongodb.png","width":400,"height":400},{"@type":"BreadcrumbList","@id":"https:\/\/huongdanjava.com\/manipulate-with-mongodb-in-java-application.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/huongdanjava.com\/"},{"@type":"ListItem","position":2,"name":"Manipulate with MongoDB in Java application"}]},{"@type":"WebSite","@id":"https:\/\/huongdanjava.com\/#website","url":"https:\/\/huongdanjava.com\/","name":"Huong Dan Java","description":"Java development tutorials","publisher":{"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/huongdanjava.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/huongdanjava.com\/#\/schema\/person\/dc859d7f8cbea3b593e6738de9cbb82d","name":"Khanh Nguyen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","url":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","contentUrl":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg","width":1267,"height":1517,"caption":"Khanh Nguyen"},"logo":{"@id":"https:\/\/huongdanjava.com\/wp-content\/uploads\/2021\/07\/CC6FAC58-D227-4DD8-93D1-6D6A795577E3_1_201_a.jpeg"},"description":"I love Java and everything related to Java.","sameAs":["https:\/\/huongdanjava.com","https:\/\/www.facebook.com\/nhkhanh2406","https:\/\/x.com\/https:\/\/twitter.com\/KhanhNguyenJ"]}]}},"_links":{"self":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/8421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/comments?post=8421"}],"version-history":[{"count":7,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/8421\/revisions"}],"predecessor-version":[{"id":8694,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/posts\/8421\/revisions\/8694"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/media\/1327"}],"wp:attachment":[{"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/media?parent=8421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/categories?post=8421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/huongdanjava.com\/wp-json\/wp\/v2\/tags?post=8421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}