Java Test Mongodb__java

Source: Internet
Author: User
Tags getmessage mongoclient mongodb name database
----------------------------------------------------Java Test MongoDB the Java code to connect to the database is as follows: Import com.mongodb.MongoClient;


Import Com.mongodb.client.MongoDatabase; public class mongodbjdbc{public static void Main (String args[]) {try{//Connect to MongoDB service Mon
       
         Goclient mongoclient = new Mongoclient ("localhost", 27017);  
       Connect to database Mongodatabase Mongodatabase = mongoclient.getdatabase ("MyCol");
        
      System.out.println ("Connect to Database successfully");
     }catch (Exception e) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ());  
The Java code for the connection database of the account password is as follows: Import java.util.ArrayList;  
Import java.util.List;  
Import com.mongodb.MongoClient;  
Import com.mongodb.MongoCredential;  
Import com.mongodb.ServerAddress;  
  
Import Com.mongodb.client.MongoDatabase; public class Mongodbjdbc {public static void main (string[] args) {try {//Connect to MongoDB service if it is far Chenglian can replace the "localhost "for server IP address//serveraddress () two parameters for server address and port serveraddress serveraddress = new Ser  
            Veraddress ("localhost", 27017);  
            list<serveraddress> Addrs = new arraylist<serveraddress> ();  
              
            Addrs.add (serveraddress); Mongocredential.createscramsha1credential () Three parameters are user name database name password mongocredential credential = Mongocredenti  
            Al.createscramsha1credential ("username", "databaseName", "Password". ToCharArray ());  
            List<mongocredential> credentials = new arraylist<mongocredential> ();  
              
            Credentials.add (credential);  
              
            Obtain MongoDB connection Mongoclient mongoclient = new Mongoclient (addrs,credentials) through connection authentication;  
            Connect to database Mongodatabase Mongodatabase = mongoclient.getdatabase ("DatabaseName");  
        System.out.println ("Connect to Database successfully"); catch (Exceptione) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ());
Create a collection: Import com.mongodb.MongoClient;


Import Com.mongodb.client.MongoDatabase; public class mongodbjdbc{public static void Main (String args[]) {try{//Connect to MongoDB service Mongocl
         
       
      Ient mongoclient = new Mongoclient ("localhost", 27017);  
      Connect to database Mongodatabase Mongodatabase = mongoclient.getdatabase ("MyCol");
      System.out.println ("Connect to Database successfully");
      Mongodatabase.createcollection ("test");
        
      System.out.println ("Collection Creation succeeded");
     }catch (Exception e) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ());
Gets the collection: import org.bson.Document;
Import com.mongodb.MongoClient;
Import com.mongodb.client.MongoCollection;


Import Com.mongodb.client.MongoDatabase;   
    public class mongodbjdbc{public static void Main (String args[]) {try{   Connect to MongoDB service mongoclient mongoclient = new Mongoclient ("localhost", 27017);  
       Connect to database Mongodatabase Mongodatabase = mongoclient.getdatabase ("MyCol");
      
       System.out.println ("Connect to Database successfully");
       Mongocollection<document> collection = mongodatabase.getcollection ("test");
      SYSTEM.OUT.PRINTLN ("Set Test selection succeeded");
     }catch (Exception e) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ());
Insert document import Java.util.ArrayList;
Import java.util.List;


Import org.bson.Document;
Import com.mongodb.MongoClient;
Import com.mongodb.client.MongoCollection;


Import Com.mongodb.client.MongoDatabase; public class mongodbjdbc{public static void Main (String args[]) {try{//Connect to MongoDB service M
         
         Ongoclient mongoclient = new Mongoclient ("localhost", 27017); Connect to database mongodatabase Mongodatabase = Mongoclient.getdatabASE ("MyCol");
         
         System.out.println ("Connect to Database successfully");
         Mongocollection<document> collection = mongodatabase.getcollection ("test");
         SYSTEM.OUT.PRINTLN ("Set Test selection succeeded"); Insert Document/** * 1. Create document org.bson.Document key-value format * 2. Create a Document collection List<document> * 3. Insert a document collection into a database collection Mongocollection.insertmany (list<document>) Inserts a single document can be used Mongocollection.insertone (document) * *  
         /Document document = new Document ("title", "MongoDB").  
         Append ("description", "Database").  
         Append ("likes", 100).  
         Append ("by", "Fly");  
         list<document> documents = new arraylist<document> ();  
         Documents.Add (document);  
         Collection.insertmany (documents);  
      SYSTEM.OUT.PRINTLN ("Document insert succeeded");
      }catch (Exception e) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ());
Retrieve all DocumentsImport org.bson.Document;
Import com.mongodb.MongoClient;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoCursor;


Import Com.mongodb.client.MongoDatabase; public class mongodbjdbc{public static void Main (String args[]) {try{//Connect to MongoDB service M
         
         Ongoclient mongoclient = new Mongoclient ("localhost", 27017);  
         Connect to database Mongodatabase Mongodatabase = mongoclient.getdatabase ("MyCol");
         
         System.out.println ("Connect to Database successfully");
         Mongocollection<document> collection = mongodatabase.getcollection ("test");
         
         SYSTEM.OUT.PRINTLN ("Set Test selection succeeded"); Retrieves all documents/** * 1. Gets the iterator finditerable<document> * 2. Gets the cursor mongocursor<document> * 3.  
         A collection of retrieved documents via a cursor * */finditerable<document> finditerable = Collection.find (); Mongocursor<document> mongocursor = Finditerable.iterator ();  
         while (Mongocursor.hasnext ()) {System.out.println (Mongocursor.next ());
      }}catch (Exception e) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ());
Update the document import Org.bson.Document;
Import com.mongodb.MongoClient;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoCursor;
Import Com.mongodb.client.MongoDatabase;


Import Com.mongodb.client.model.Filters; public class mongodbjdbc{public static void Main (String args[]) {try{//Connect to MongoDB service M
         
         Ongoclient mongoclient = new Mongoclient ("localhost", 27017);  
         Connect to database Mongodatabase Mongodatabase = mongoclient.getdatabase ("MyCol");
         
         System.out.println ("Connect to Database successfully"); Mongocollection<document> collection = MongodatAbase.getcollection ("test");
         
         SYSTEM.OUT.PRINTLN ("Set Test selection succeeded"); Update the document modifies the LIKES=100 document in the document to likes=200 Collection.updatemany (Filters.eq ("likes"), New Document ("$set", new  
         Document ("likes", 200));  
         Retrieve view results finditerable<document> finditerable = Collection.find ();  
         mongocursor<document> mongocursor = Finditerable.iterator ();  
         while (Mongocursor.hasnext ()) {System.out.println (Mongocursor.next ());
      }}catch (Exception e) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ());
Delete the document import org.bson.Document;
Import com.mongodb.MongoClient;
Import com.mongodb.client.FindIterable;
Import com.mongodb.client.MongoCollection;
Import Com.mongodb.client.MongoCursor;
Import Com.mongodb.client.MongoDatabase;


Import Com.mongodb.client.model.Filters;
      public class mongodbjdbc{public static void Main (String args[]) {try{//connection to MongoDB service mongoclient mongoclient = new Mongoclient ("localhost", 27017);  
         Connect to database Mongodatabase Mongodatabase = mongoclient.getdatabase ("MyCol");


         System.out.println ("Connect to Database successfully");
         Mongocollection<document> collection = mongodatabase.getcollection ("test");


         SYSTEM.OUT.PRINTLN ("Set Test selection succeeded");  
         Deletes the first document that meets the criteria Collection.deleteone (Filters.eq ("likes", 200));  
         Delete all eligible documents Collection.deletemany (FILTERS.EQ ("likes", 200));  
         Retrieve view results finditerable<document> finditerable = Collection.find ();  
         mongocursor<document> mongocursor = Finditerable.iterator ();  
         while (Mongocursor.hasnext ()) {System.out.println (Mongocursor.next ());
     }}catch (Exception e) {System.err.println (E.getclass (). GetName () + ":" + e.getmessage ()); }
   }
}

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.