MongoDB and Java integration need to use the MongoDB driver, if it is MAVEN environment, then add the following dependency:
<Dependency> <groupId>Org.mongodb</groupId> <Artifactid>Mongo-java-driver</Artifactid> <version>3.2.2</version></Dependency>
If it is an ordinary project, download the corresponding jar package and add it to the Lib directory.
ServerAddress serveraddress =NewServerAddress ("localhost", 27017);
List<ServerAddress> addresses =NewArraylist<serveraddress>(); Addresses.add (serveraddress); //Mogodb Open authentication of permissions//similar to Db.auth ("UserName", "pwd")Mongocredential credential = mongocredential.createcredential ("root", "test", "000000". ToCharArray ()); List<MongoCredential> credentials =NewArraylist<mongocredential>(); Credentials.add (credential); Mongoclient Client=NewMongoclient (addresses, credentials); //linking to a database//Use testMongodatabase mongodatabase = client.getdatabase ("Test"); Log.info ("Create Link succeeded"); //Get Collectionmongocollection<document> item = mongodatabase.getcollection ("Item_info"); //inserting elements in a collection like /*** 1. Create document org.bson.Document parameter Key-value format * 2. Create a document collection List<document> * 3. Insert a document collection into the Database collection Mongocollection.insertmany (list<document>) Inserts a single document can be used with the Mongocollection.insertone (document) * */List<Document> documents =NewArraylist<document>(); Long Starttimestamp=System.currenttimemillis (); for(inti = 0; I < 10; i++) {Document document=NewDocument ("item_id", Uuid.randomuuid (). toString ()). Append ("Item_name", "itemname_" +i). Append ("SKU", "sku_" +i). Append ("Price", Double.parsedouble ("5") +i). Append ("Desc", "desc_" + "item_name_" +i); //Documents.Add (document);Item.insertone (document); }//Item.insertmany (documents);Long Endtimestamp =System.currenttimemillis (); System.out.println ("============="); System.out.println ("Time-consuming:" + ((Endtimestamp-starttimestamp))/1000.0); System.out.println ("============="); //Enquiry//Bson condition =//Search for items with a price less than 200finditerable<document> results = Item.find (filters.lt ("Price", 10)); Mongocursor<Document> iterator =Results.iterator (); inti = 0; if(NULL! = Results &&Iterator.hasnext ()) { while(Iterator.hasnext ()) {System.out.println ("==================="); Document result=Iterator.next (); System.out.println (Result.get ("ITEM_ID")); System.out.println (Result.get ("Item_name")); System.out.println (Result.get ("Price")); System.out.println ("==================="); I++; } } //total number of checksSystem.out.println (i++); System.out.println ("<--------------------------->\n"); //Update Action//The update operation is for the function above the collection,Updateresult s = Item.updatemany (filters.lt ("Price", 10),NewDocument ("$set", NewDocument (). Append ("Price", 11.5). Append ("Item_name", "Update_item_name"))); //Enquiryfinditerable<document> Updateitems = Item.find (Filters.eq ("Item_name", "Update_item_name")); Mongocursor<Document> Updateiterator =Updateitems.iterator (); I= 0; if(NULL!=updateiterator) { while(Updateiterator.hasnext ()) {Document UpdateItem=Updateiterator.next (); System.out.println ("==================="); System.out.println (Updateitem.get ("ITEM_ID")); System.out.println (Updateitem.get ("Item_name")); System.out.println (Updateitem.get ("Price")); System.out.println ("==================="); I++; }} System.out.println (S.getmodifiedcount ()); //DeleteDeleteresult Delete = Item.deletemany (Filters.eq ("Item_name", "Update_item_name")); System.out.println (Delete.getdeletedcount ());
MongoDB and Java Integration