Hello MongoDB--Java programming

Source: Internet
Author: User
Tags mongoclient

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.