MongoDB Quick Start Learning Note 8 MongoDB Java driver operation

Source: Internet
Author: User
Tags mongoclient name database

Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.regex.Pattern;Importorg.bson.Document;Importcom.mongodb.MongoClient;Importcom.mongodb.MongoCredential;Importcom.mongodb.ServerAddress;Importcom.mongodb.client.FindIterable;Importcom.mongodb.client.MongoCollection;ImportCom.mongodb.client.MongoCursor;Importcom.mongodb.client.MongoDatabase; Public classMongozyh { Public Static voidMain (string[] args) {Try {            //Connect to a MongoDB service, serveraddress () two parameters for server address and port, respectivelyServerAddress serveraddress =NewServerAddress ("localhost", 27017); List<ServerAddress> Addrs =NewArraylist<serveraddress>();            Addrs.add (serveraddress); //three parameters for user name database name passwordMongocredential credential =mongocredential. Createscramsha1credential ("Zyh", "admin",                            "Zyh". ToCharArray ()); List<MongoCredential> credentials =NewArraylist<mongocredential>();            Credentials.add (credential); //obtaining MongoDB connections through connection authenticationMongoclient mongoclient =Newmongoclient (Addrs, credentials); //connecting to a databaseMongodatabase mongodatabase = mongoclient.getdatabase ("Zyhdb"); //Creates a new collection and creates a new empty collection in the database after execution//mongodatabase.createcollection ("student"); //System.out.println ("new collection succeeded"); //gets the collection and inserts data into the collectionMongocollection<document> mongocollection =mongodatabase. GetCollection ("Student"); //Insert a piece of data//Document document = new document (); //document.append ("name", "Zhangsan"); //document.append ("Age", 28); //Mongocollection.insertone (document); //System.out.println ("Insert a piece of data successfully"); //inserting more than one data//list<document> documentlist = new arraylist<document> (); //Document Document1 = new document (); //document1.append ("name", "Lisi"); //document1.append ("Age", 28); //document1.append ("Sex", "male"); //Document Document2 = new document (); //document2.append ("name", "Wangwu"); //document2.append ("Age", 31); //document2.append ("Sex", "male"); //Documentlist.add (Document1); //Documentlist.add (Document2); //Mongocollection.insertmany (documentlist); //System.out.println ("Insert multiple data Success"); //Querying Data//querying all the data in a collection//finditerable<document> finditerable = Mongocollection.find (); //mongocursor<document> mongocursor = Finditerable.iterator (); //While (Mongocursor.hasnext ()) {//System.out.println (Mongocursor.next ()); // }            //Query by Condition//Document query = new document (); //query.put ("Age", New Document ("$lt", 30)); //query.put ("Sex", "male"); //query.put ("name", query); //Regular Expression Query//Pattern pattern = pattern.compile ("^zhang"); //query.put ("name", pattern); //Sort//Document sort = new document (); //sort.put ("name",-1);//1 is the positive sequence,-1 is reverse//finditerable<document> finditerable = mongocollection.find (query)//. Sort (sort); //mongocursor<document> mongocursor = Finditerable.iterator (); //While (Mongocursor.hasnext ()) {//Document doc = Mongocursor.next (); //System.out.print ("Name:" + doc.get ("name") + "..."); //System.out.print ("Age:" + doc.get ("age") + "..."); //System.out.println ("Sex:" + doc.get ("sex") + "..."); // }            //mongocollection.findoneandupdate (query condition, modify content);//query out the first data and modify//mongocollection.findoneanddelete (query conditions);//query out the first data and delete//mongocollection.findoneandreplace (query condition, replace content);//query out the first data and replace//Modifying Data//Document query = new document (); //query.put ("Age", 28); //Document update = new document (); //Document D = new document (); //d.put ("Birthday", New Date ()); //d.put ("name", "Zhangsan"); //update.put ("$set", D); //mongocollection.updateone (query, update);//Modify the first piece of data to be queried//mongocollection.updatemany (query condition, modify content);//Modify all the data that is queried//Delete Data//Document query = new document (); //query.put ("Age", 28); //mongocollection.deleteone (query);//Delete the first piece of data to the query//mongocollection.deletemany (query conditions);//Delete all data that is queried//Mongocollection.drop ();//Delete Collection        } Catch(Exception e) {e.printstacktrace (); }    }}

MongoDB Quick Start Learning Note 8 MongoDB Java driver operation

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.