Environment configuration: If you want to use MongoDB in a Java program, you need to make sure that you have installed the Java environment and the MongoDB JDBC driver.
First you must download the MONGO Jar package: http://mongodb.github.io/mongo-java-driver/, make sure to download the latest version.
https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/
Hello MongoDB-Java Programming Demo:
PackageCom.xctdemo.main;Importjava.util.ArrayList;Importjava.util.List;Importorg.bson.Document;Importcom.mongodb.MongoClient;Importcom.mongodb.client.FindIterable;Importcom.mongodb.client.MongoCollection;ImportCom.mongodb.client.MongoCursor;Importcom.mongodb.client.MongoDatabase;Importcom.mongodb.client.model.Filters; Public classHellomongodb { Public Static voidMain (string[] args) {System.out.println ("----Hello MongoDB----"); Try { //Connect to MongoDB serviceMongoclient mongoclient =NewMongoclient ("localhost", 27017); //connecting to a databaseMongodatabase mongodatabase = Mongoclient.getdatabase ("World"); System.out.println ("Connect to database successfully"); /** mongodatabase.createcollection ("city"); * SYSTEM.OUT.PRINTLN ("Collection City Creation Success"); // */ //Get CollectionMongocollection<document> collection =mongodatabase. GetCollection ("City"); System.out.println ("Collection City selected successfully"); //inserting a document in a collection /*** 1. Create a document org.bson.Document parameter Key-value format 2. Create a Document collection List<document> * 3. Inserting a collection of documents into a database collection Mongocollection.insertmany (list<document>) * Insert a single document can be used Mongocollection.insertone */Document Document=NewDocument ("name", "Beijing"). Append ("Country", "China"). Append ("Provice", "Beijing"). Append ("People-count", 1000); List<Document> documents =NewArraylist<document>(); Documents.Add (document); Collection.insertmany (documents); System.out.println ("Document Insertion succeeded-many"); Document mydocument=NewDocument ("name", "Washington"). Append ("Country", "America"). Append ("Provice", "WASHINGTON"). Append ("People-count", 1200); Collection.insertone (mydocument); System.out.println ("Document Insertion succeeded-one"); //Retrieving all DocumentsShowdocuments (collection); //Delete the first document that matches a condition//Collection.deleteone (Filters.eq ("name", "Beijing")); //Delete all documents that match the criteriaCollection.deletemany (Filters.eq ("name", "Beijing")); System.out.println ("----deleted OK----"); System.out.println ("--------------------------"); //update the document to modify the document in document LIKES=100 to likes=200Collection.updatemany (Filters.eq ("name", "Washington"), NewDocument ("$set",NewDocument ("Country", "JAPAN"))); //Retrieving all DocumentsShowdocuments (collection); } Catch(Exception e) {System.err.println (E.getclass (). GetName ()+ ": " +e.getmessage ()); } System.out.println ("----over----"); } Private Static voidShowdocuments (mongocollection<document>collection) { /*** 1. Get iterator finditerable<document> 2. Gets the cursor mongocursor<document> 3. * Traversal of retrieved document collections via cursors **/finditerable<Document> finditerable =Collection.find (); Mongocursor<Document> Mongocursor =Finditerable.iterator (); while(Mongocursor.hasnext ()) {System.out.println (Mongocursor.next ());//. Get ("name") } }}
Hello MongoDB--Java programming