{"id":2770,"date":"2021-07-20T11:34:53","date_gmt":"2021-07-20T11:34:53","guid":{"rendered":"https:\/\/www.bearloga.space\/?page_id=2770"},"modified":"2021-07-20T11:34:54","modified_gmt":"2021-07-20T11:34:54","slug":"mongodb-java","status":"publish","type":"page","link":"https:\/\/www.bearloga.space\/uk\/mongodb-java\/","title":{"rendered":"MongoDB \u2014 Java"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u0412 \u044d\u0442\u043e\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u0443\u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0440\u0430\u0439\u0432\u0435\u0440 JDBC MongoDB.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u041c\u043e\u043d\u0442\u0430\u0436<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0432\u044b \u043d\u0430\u0447\u043d\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c MongoDB \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445 \u043d\u0430 Java, \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043d\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u0434\u0440\u0430\u0439\u0432\u0435\u0440 JDBC MongoDB \u0438 Java.&nbsp;\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0443\u0447\u0435\u0431\u043d\u0438\u043a Java \u0434\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 Java \u043d\u0430 \u0432\u0430\u0448\u0435\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435.&nbsp;\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0440\u0430\u0439\u0432\u0435\u0440 JDBC MongoDB.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u0412\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0431\u0430\u043d\u043a\u0443 \u0441 \u043f\u0443\u0442\u0438&nbsp;<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/translate.googleusercontent.com\/translate_c?depth=1&amp;rurl=translate.google.ru&amp;sl=en&amp;sp=nmt4&amp;tl=ru&amp;u=https:\/\/github.com\/mongodb\/mongo-java-driver\/downloads&amp;xid=17259,15700022,15700186,15700190,15700256,15700259,15700262&amp;usg=ALkJrhgG8O5licCZQP5DHXPq_MC32gpU5w\">\u0421\u043a\u0430\u0447\u0430\u0442\u044c mongo.jar<\/a>&nbsp;.&nbsp;\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0432\u0435\u0440\u0441\u0438\u044e.<\/li><li>\u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c mongo.jar \u0432 \u0432\u0430\u0448 \u043f\u0443\u0442\u044c \u043a \u043a\u043b\u0430\u0441\u0441\u0430\u043c.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0431\u0430\u043d\u043a\u0443 \u0441 \u043f\u0443\u0442\u0438&nbsp;<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/translate.googleusercontent.com\/translate_c?depth=1&amp;rurl=translate.google.ru&amp;sl=en&amp;sp=nmt4&amp;tl=ru&amp;u=https:\/\/github.com\/mongodb\/mongo-java-driver\/downloads&amp;xid=17259,15700022,15700186,15700190,15700256,15700259,15700262&amp;usg=ALkJrhgG8O5licCZQP5DHXPq_MC32gpU5w\">\u0421\u043a\u0430\u0447\u0430\u0442\u044c mongo.jar<\/a>&nbsp;.&nbsp;\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0432\u0435\u0440\u0441\u0438\u044e.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c mongo.jar \u0432 \u0432\u0430\u0448 \u043f\u0443\u0442\u044c \u043a \u043a\u043b\u0430\u0441\u0441\u0430\u043c.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0438\u043c\u044f \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0435\u0441\u043b\u0438 \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, MongoDB \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0435\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.MongoDatabase; \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class ConnectToDB { \n   \n   public static void main( String args[] ) {  \n      \n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 ); \n   \n      \/\/ Creating Credentials \n      MongoCredential credential; \n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/ Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\"); \n      System.out.println(\"Credentials ::\"+ credential);     \n   } \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 myDb, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0438\u0436\u0435.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$javac ConnectToDB.java \n$java ConnectToDB\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u044b\u0448\u0435\u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0432\u044b\u0434\u0430\u0441\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u044b\u0432\u043e\u0434.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connected to the database successfully \nCredentials ::MongoCredential{\n   mechanism = null, \n   userName = 'sampleUser', \n   source = 'myDb', \n   password = &lt;hidden&gt;, \n   mechanismProperties = {}\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438&nbsp;<strong>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f<\/strong>&nbsp;\u043c\u0435\u0442\u043e\u0434&nbsp;<strong>createCollection ()<\/strong>&nbsp;\u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>com.mongodb.client.MongoDatabase<\/strong>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.MongoDatabase; \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class CreatingCollection { \n   \n   public static void main( String args[] ) {  \n      \n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 ); \n     \n      \/\/ Creating Credentials \n      MongoCredential credential; \n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\");  \n      \n      \/\/Creating a collection \n      database.createCollection(\"sampleCollection\"); \n      System.out.println(\"Collection created successfully\"); \n   } \n} <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connected to the database successfully \nCollection created successfully\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \/ \u0412\u044b\u0431\u043e\u0440 \u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \/ \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445,&nbsp;<strong>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f<\/strong>&nbsp;\u043c\u0435\u0442\u043e\u0434&nbsp;<strong>getCollection ()<\/strong>&nbsp;\u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>com.mongodb.client.MongoDatabase<\/strong>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \/ \u0432\u044b\u0431\u043e\u0440\u0430 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.MongoCollection; \nimport com.mongodb.client.MongoDatabase; \n\nimport org.bson.Document; \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class selectingCollection { \n   \n   public static void main( String args[] ) {  \n      \n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 ); \n     \n      \/\/ Creating Credentials \n      MongoCredential credential; \n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/ Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\");  \n      \n      \/\/ Creating a collection \n      System.out.println(\"Collection created successfully\"); \n\n      \/\/ Retieving a collection\n      MongoCollection&lt;Document&gt; collection = database.getCollection(\"myCollection\"); \n      System.out.println(\"Collection myCollection selected successfully\"); \n   }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connected to the database successfully \nCollection created successfully \nCollection myCollection selected successfully\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0432 MongoDB, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434&nbsp;<strong>insert ()<\/strong>&nbsp;\u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>com.mongodb.client.MongoCollection<\/strong>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.MongoCollection; \nimport com.mongodb.client.MongoDatabase; \n\nimport org.bson.Document;  \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class InsertingDocument { \n   \n   public static void main( String args[] ) {  \n      \n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 ); \n\n      \/\/ Creating Credentials \n      MongoCredential credential; \n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/ Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\"); \n\n      \/\/ Retrieving a collection\n      MongoCollection&lt;Document&gt; collection = database.getCollection(\"sampleCollection\"); \n      System.out.println(\"Collection sampleCollection selected successfully\");\n\n      Document document = new Document(\"title\", \"MongoDB\") \n      .append(\"id\", 1)\n      .append(\"description\", \"database\") \n      .append(\"likes\", 100) \n      .append(\"url\", \"http:\/\/www.tutorialspoint.com\/mongodb\/\") \n      .append(\"by\", \"tutorials point\");  \n      collection.insertOne(document); \n      System.out.println(\"Document inserted successfully\");     \n   } \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connected to the database successfully \nCollection sampleCollection selected successfully \nDocument inserted successfully\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043c\u0435\u0442\u043e\u0434&nbsp;<strong>find ()<\/strong>&nbsp;\u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>com.mongodb.client.MongoCollection<\/strong>&nbsp;.&nbsp;\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0443\u0440\u0441\u043e\u0440, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u0443\u0440\u0441\u043e\u0440.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u0432\u0441\u0435\u0445 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.FindIterable; \nimport com.mongodb.client.MongoCollection; \nimport com.mongodb.client.MongoDatabase;  \n\nimport java.util.Iterator; \nimport org.bson.Document; \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class RetrievingAllDocuments { \n   \n   public static void main( String args[] ) {  \n      \n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 ); \n\n      \/\/ Creating Credentials \n      MongoCredential credential;\n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/ Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\");  \n      \n      \/\/ Retrieving a collection \n      MongoCollection&lt;Document&gt; collection = database.getCollection(\"sampleCollection\");\n      System.out.println(\"Collection sampleCollection selected successfully\"); \n\n      \/\/ Getting the iterable object \n      FindIterable&lt;Document&gt; iterDoc = collection.find(); \n      int i = 1; \n\n      \/\/ Getting the iterator \n      Iterator it = iterDoc.iterator(); \n    \n      while (it.hasNext()) {  \n         System.out.println(it.next());  \n      i++; \n      }\n   } \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Document{{\n   _id = 5967745223993a32646baab8, \n   title = MongoDB, \n   id = 1, \n   description = database, \n   likes = 100, \n   url = http:\/\/www.tutorialspoint.com\/mongodb\/, by = tutorials point\n}}  \nDocument{{\n   _id = 7452239959673a32646baab8, \n   title = RethinkDB, \n   id = 2, \n   description = database, \n   likes = 200, \n   url = http:\/\/www.tutorialspoint.com\/rethinkdb\/, by = tutorials point\n}}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0414\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438&nbsp;<strong>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f<\/strong>&nbsp;\u043c\u0435\u0442\u043e\u0434&nbsp;<strong>updateOne ()<\/strong>&nbsp;\u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>com.mongodb.client.MongoCollection<\/strong>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.FindIterable; \nimport com.mongodb.client.MongoCollection; \nimport com.mongodb.client.MongoDatabase; \nimport com.mongodb.client.model.Filters; \nimport com.mongodb.client.model.Updates; \n\nimport java.util.Iterator; \nimport org.bson.Document;  \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class UpdatingDocuments { \n   \n   public static void main( String args[] ) {  \n      \n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 ); \n     \n      \/\/ Creating Credentials \n      MongoCredential credential; \n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/ Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\"); \n\n      \/\/ Retrieving a collection \n      MongoCollection&lt;Document&gt; collection = database.getCollection(\"sampleCollection\");\n      System.out.println(\"Collection myCollection selected successfully\"); \n\n      collection.updateOne(Filters.eq(\"id\", 1), Updates.set(\"likes\", 150));       \n      System.out.println(\"Document update successfully...\");  \n      \n      \/\/ Retrieving the documents after updation \n      \/\/ Getting the iterable object\n      FindIterable&lt;Document&gt; iterDoc = collection.find(); \n      int i = 1; \n\n      \/\/ Getting the iterator \n      Iterator it = iterDoc.iterator(); \n\n      while (it.hasNext()) {  \n         System.out.println(it.next());  \n         i++; \n      }     \n   }  \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Document update successfully... \nDocument {{\n   _id = 5967745223993a32646baab8, \n   title = MongoDB, \n   id = 1, \n   description = database, \n   likes = 150, \n   url = http:\/\/www.tutorialspoint.com\/mongodb\/, by = tutorials point\n}}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434&nbsp;<strong>deleteOne ()<\/strong>&nbsp;\u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>com.mongodb.client.MongoCollection<\/strong>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.FindIterable; \nimport com.mongodb.client.MongoCollection; \nimport com.mongodb.client.MongoDatabase; \nimport com.mongodb.client.model.Filters;  \n\nimport java.util.Iterator; \nimport org.bson.Document; \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class DeletingDocuments { \n   \n   public static void main( String args[] ) {  \n   \n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 );\n      \n      \/\/ Creating Credentials \n      MongoCredential credential; \n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/ Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\"); \n\n      \/\/ Retrieving a collection\n      MongoCollection&lt;Document&gt; collection = database.getCollection(\"sampleCollection\");\n      System.out.println(\"Collection sampleCollection selected successfully\"); \n\n      \/\/ Deleting the documents \n      collection.deleteOne(Filters.eq(\"id\", 1)); \n      System.out.println(\"Document deleted successfully...\");  \n      \n      \/\/ Retrieving the documents after updation \n      \/\/ Getting the iterable object \n      FindIterable&lt;Document&gt; iterDoc = collection.find(); \n      int i = 1; \n\n      \/\/ Getting the iterator \n      Iterator it = iterDoc.iterator(); \n\n      while (it.hasNext()) {  \n         System.out.println(\"Inserted Document: \"+i);  \n         System.out.println(it.next());  \n         i++; \n      }       \n   } \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connected to the database successfully \nCollection sampleCollection selected successfully \nDocument deleted successfully...\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u041e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434&nbsp;<strong>drop ()<\/strong>&nbsp;\u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>com.mongodb.client.MongoCollection<\/strong>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.MongoCollection; \nimport com.mongodb.client.MongoDatabase;  \n\nimport org.bson.Document;  \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class DropingCollection { \n   \n   public static void main( String args[] ) {  \n\n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 ); \n\n      \/\/ Creating Credentials \n      MongoCredential credential; \n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/ Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\");  \n      \n      \/\/ Creating a collection \n      System.out.println(\"Collections created successfully\"); \n\n      \/\/ Retieving a collection\n      MongoCollection&lt;Document&gt; collection = database.getCollection(\"sampleCollection\");\n\n      \/\/ Dropping a Collection \n      collection.drop(); \n      System.out.println(\"Collection dropped successfully\");\n   } \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connected to the database successfully \nCollection sampleCollection selected successfully \nCollection dropped successfully\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434&nbsp;<strong>listCollectionNames ()<\/strong>&nbsp;\u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>com.mongodb.client.MongoDatabase<\/strong>&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.mongodb.client.MongoDatabase; \nimport com.mongodb.MongoClient; \nimport com.mongodb.MongoCredential;  \n\npublic class ListOfCollection { \n   \n   public static void main( String args[] ) {  \n      \n      \/\/ Creating a Mongo client \n      MongoClient mongo = new MongoClient( \"localhost\" , 27017 ); \n\n      \/\/ Creating Credentials \n      MongoCredential credential; \n      credential = MongoCredential.createCredential(\"sampleUser\", \"myDb\", \n         \"password\".toCharArray()); \n\n      System.out.println(\"Connected to the database successfully\");  \n      \n      \/\/ Accessing the database \n      MongoDatabase database = mongo.getDatabase(\"myDb\"); \n      System.out.println(\"Collection created successfully\"); \n      for (String name : database.listCollectionNames()) { \n         System.out.println(name); \n      } \n   }\n} <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Connected to the database successfully \nCollection created successfully \nmyCollection \nmyCollection1 \nmyCollection5\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u044b MongoDB&nbsp;<strong>save (), limit (), skip (), sort ()<\/strong>&nbsp;\u0438 \u0442. \u0414. \u0420\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0443\u0440\u043e\u043a\u0435.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 \u044d\u0442\u043e\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u0443\u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0440\u0430\u0439\u0432\u0435\u0440 JDBC MongoDB. \u041c\u043e\u043d\u0442\u0430\u0436 \u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0432\u044b \u043d\u0430\u0447\u043d\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c MongoDB \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-2770","page","type-page","status-publish","hentry","category-mongodb"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/2770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/comments?post=2770"}],"version-history":[{"count":0,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/2770\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/media?parent=2770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/categories?post=2770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/tags?post=2770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}