WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Java


MongoDB - Java : we are able to learn how to installation MongoDB JDBC driver.

Installation

First you need to make certain which you have MongoDB JDBC driving force and Java set up in computer.You can test Java tutorial for Java set up in your machine.Now, let us check the way to installation MongoDB JDBC driver.

Connect to Database

To join database, you want to specify the database call, if the database does not exist then MongoDB creates it automatically. There are Following is the code snippet to hook up with the database
import com.mongodb.client.MongoDatabase; 
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential;  

public class ConnectToDB  
   
   public static void main( String args[] )   
      
      // Creating a Mongo client 
      MongoClient mongo = new MongoClient( "localhost" , 27017 ); 
   
      // Creating Credentials 
      MongoCredential credential; 
      credential = MongoCredential.createCredential("sampleUser", "myDb", 
         "password".toCharArray()); 
      System.out.println("Connected to the database correctly");  
      
      // Accessing the database 
      MongoDatabase database = mongo.getDatabase("myDb"); 
      System.out.println("Credentials ::"+ credential);     
    
when collect and run the above software to create our database myDb as proven
$javac ConnectToDB.Java 
$java ConnectToDB

But the above application gives you the following output.
Connected to the database successfully 
Credentials ::MongoCredential
   mechanism = null, 
   userName = 'sampleUser', 
   supply = 'myDb', 
   password = <hidden>, 
   mechanismProperties = 

Create a Collection

To create a set, createCollection() technique of com.Mongodb.Purchaser.MongoDatabase magnificence is used.
import com.mongodb.patron.MongoDatabase; 
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential;  

public magnificence CreatingCollection  
   
   public static void principal( String args[] )   
      
      // Creating a Mongo client 
      MongoClient mongo = new MongoClient( "localhost" , 27017 ); 
     
      // Creating Credentials 
      MongoCredential credential; 
      credential = MongoCredential.createCredential("sampleUser", "myDb", 
         "password".toCharArray()); 
      System.out.println("Connected to the database efficaciously");  
      
      //Accessing the database 
      MongoDatabase database = mongo.getDatabase("myDb");  
      
      //Creating a set 
      database.createCollection("sampleCollection"); 
      System.out.println("Collection created efficaciously"); 
    
 
When the above program gives you the following result
Connected to the database successfully 
Collection created efficaciously

Getting/Selecting a Collection

To get/select a collection from the database, getCollection() approach of com.Mongodb.Consumer.MongoDatabase class is used.
import com.mongodb.consumer.MongoCollection; 
import com.mongodb.client.MongoDatabase; 

import org.bson.Document; 
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential;  

public elegance selectingCollection  
   
   public static void principal( String args[] )   
      
      // Creating a Mongo customer 
      MongoClient mongo = new MongoClient( "localhost" , 27017 ); 
     
      // Creating Credentials 
      MongoCredential credential; 
      credential = MongoCredential.createCredential("sampleUser", "myDb", 
         "password".toCharArray()); 
      System.out.println("Connected to the database successfully");  
      
      // Accessing the database 
      MongoDatabase database = mongo.getDatabase("myDb");  
      
      // Creating a group 
      System.out.println("Collection created successfully"); 

      // Retieving a collection
      MongoCollection<Document> collection = database.getCollection("myCollection"); 
      System.out.println("Collection myCollection selected correctly"); 
   
On compiling, the above program gives you the following end result
Connected to the database efficiently 
Collection created efficaciously 
Collection myCollection selected efficiently

Insert a Document

To insert a document into MongoDB, insert() approach of com.Mongodb.Patron.MongoCollection magnificence is used.
import com.Mongodb.Client.MongoCollection;
import com.Mongodb.Consumer.MongoDatabase;

import org.Bson.Document;
import com.Mongodb.MongoClient;
import com.Mongodb.MongoCredential;

public class InsertingDocument 

    public static void principal( String args[] ) 

        // Creating a Mongo purchaser
        MongoClient mongo = new MongoClient( "localhost" , 27017 );

        // Creating Credentials
        MongoCredential credential;
        credential = MongoCredential.CreateCredential("sampleUser", "myDb",
            "password".ToCharArray());
        System.Out.Println("Connected to the database efficiently");

        // Accessing the database
        MongoDatabase database = mongo.GetDatabase("myDb");

        // Retrieving a collection
        MongoCollection collection = database.GetCollection("sampleCollection");
        System.Out.Println("Collection sampleCollection selected successfully");

        Document report = new Document("title", "MongoDB")
        .Append("id", 1)
        .Append("description", "database")
        .Append("vote", 88)
        .Append("url", "http://welookups.Com/mongodb/")
        .Append("by way of", "tutorials factor");
        series.InsertOne(report);
        System.Out.Println("Document inserted efficiently");
    

here the above software offers you the following result
Connected to the database efficiently 
Collection sampleCollection decided on efficaciously 
Document inserted effectively

Retrieve All Documents

To pick out all files from the gathering, discover() approach of com.Mongodb.Purchaser.MongoCollection magnificence is used.
import com.mongodb.client.FindIterable; 
import com.mongodb.customer.MongoCollection; 
import com.mongodb.patron.MongoDatabase;  

import java.util.Iterator; 
import org.bson.Document; 
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential;  

public magnificence RetrievingAllDocuments  
   
   public static void most important( String args[] )   
      
      // Creating a Mongo consumer 
      MongoClient mongo = new MongoClient( "localhost" , 27017 ); 

      // Creating Credentials 
      MongoCredential credential;
      credential = MongoCredential.createCredential("sampleUser", "myDb", 
         "password".toCharArray()); 
      System.out.println("Connected to the database efficaciously");  
      
      // Accessing the database 
      MongoDatabase database = mongo.getDatabase("myDb");  
      
      // Retrieving a collection 
      MongoCollection<Document> collection = database.getCollection("sampleCollection");
      System.out.println("Collection sampleCollection decided on efficiently"); 

      // Getting the iterable item 
      FindIterable<Document> iterDoc = collection.find(); 
      int i = 1; 

      // Getting the iterator 
      Iterator it = iterDoc.iterator(); 
    
      while (it.hasNext())   
         System.out.println(it.next());  
      i++; 
      
    
the above program offers you the subsequent end result

Document
   _id = 904360, 
   name = MongoDB, 
   id = 1, 
   description = database, 
   likes = one hundred, 
   url = http://www.Welookups.Com/mongodb/, by way of = welookups
  
Document
   _id = 2345343545, 
   identify = RethinkDB, 
   identity = 2, 
   description = database, 
   likes = two hundred, 
   url = http://www.Welookups.Com, by using = welookups


Update Document

To update a report from the collection, updateOne() method of com.Mongodb.Patron.MongoCollection elegance is used.
import com.mongodb.patron.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.patron.MongoDatabase;
import com.mongodb.patron.version.Filters;
import com.mongodb.client.version.Updates;

import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;

public class UpdatingDocuments 

    public static void important( String args[] ) 

        // Creating a Mongo client 
        MongoClient mongo = new MongoClient( "localhost" , 27017 );

        // Creating Credentials 
        MongoCredential credential;
        credential = MongoCredential.createCredential("sampleUser", "myDb",
            "password".toCharArray());
        System.out.println("Connected to the database efficaciously");

        // Accessing the database 
        MongoDatabase database = mongo.getDatabase("myDb");

        // Retrieving a group 
        MongoCollection<Document> series = database.getCollection("sampleCollection");
        System.out.println("Collection myCollection decided on correctly");

        series.updateOne(Filters.eq("identification", 1), Updates.set("votes", a hundred and fifty));
        System.out.println("Document update successfully...");

        // Retrieving the documents after updation 
        // Getting the iterable object
        FindIterable<Document> iterDoc = series.discover();
        int i = 1;

        // Getting the iterator 
        Iterator it = iterDoc.iterator();

        whilst (it.hasNext()) 
            System.out.println(it.subsequent());
            i++;
        
    
the above software offers you the following end result
Document replace effectively... 
Document 
   _id = 14545145421, 
   name = MongoDB, 
   identification = 1, 
   description = database, 
   likes = 1250, 
   url = http://www.Welookups.Com/mongodb/, by using = welookups


Delete a Document

To delete a report from the collection, you need to apply the deleteOne() approach of the com.Mongodb.Purchaser.MongoCollection elegance.
import com.mongodb.consumer.FindIterable; 
import com.mongodb.client.MongoCollection; 
import com.mongodb.purchaser.MongoDatabase; 
import com.mongodb.client.version.Filters;  

import java.util.Iterator; 
import org.bson.Document; 
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential;  

public magnificence DeletingDocuments  
   
   public static void principal( String args[] )   
   
      // Creating a Mongo client 
      MongoClient mongo = new MongoClient( "localhost" , 27017 );
      
      // Creating Credentials 
      MongoCredential credential; 
      credential = MongoCredential.createCredential("sampleUser", "myDb", 
         "password".toCharArray()); 
      System.out.println("Connected to the database correctly");  
      
      // Accessing the database 
      MongoDatabase database = mongo.getDatabase("myDb"); 

      // Retrieving a set
      MongoCollection<Document> series = database.getCollection("sampleCollection");
      System.out.println("Collection sampleCollection decided on effectively"); 

      // Deleting the documents 
      collection.deleteOne(Filters.eq("identity", 1)); 
      System.out.println("Document deleted efficaciously...");  
      
      // Retrieving the documents after updation 
      // Getting the iterable item 
      FindIterable<Document> iterDoc = series.find(); 
      int i = 1; 

      // Getting the iterator 
      Iterator it = iterDoc.iterator(); 

      whilst (it.hasNext())   
         System.out.println("Inserted Document: "+i);  
         System.out.println(it.subsequent());  
         i++; 
             
    
the above software offers you the subsequent result.
Connected to the database successfully 
Collection sampleCollection selected correctly 
Document deleted efficiently...

Dropping a Collection

To drop a set from a database, you need to apply the drop() approach of the com.Mongodb.Customer.MongoCollection magnificence.
import com.mongodb.customer.MongoCollection; 
import com.mongodb.client.MongoDatabase;  

import org.bson.Document;  
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential;  

public elegance DropingCollection  
   
   public static void essential( String args[] )   

      // Creating a Mongo customer 
      MongoClient mongo = new MongoClient( "localhost" , 27017 ); 

      // Creating Credentials 
      MongoCredential credential; 
      credential = MongoCredential.createCredential("sampleUser", "myDb", 
         "password".toCharArray()); 
      System.out.println("Connected to the database correctly");  
      
      // Accessing the database 
      MongoDatabase database = mongo.getDatabase("myDb");  
      
      // Creating a group 
      System.out.println("Collections created efficiently"); 

      // Retieving a group
      MongoCollection<Document> series = database.getCollection("sampleCollection");

      // Dropping a Collection 
      collection.drop(); 
      System.out.println("Collection dropped efficaciously");
    
Connected to the database effectively 
Collection sampleCollection decided on effectively 
Collection dropped successfully

Listing All the Collections

To listing all the collections in a database, you need to use the listCollectionNames() method of the com.Mongodb.Purchaser.MongoDatabase elegance.

import com.mongodb.purchaser.MongoDatabase; 
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential;  

public class ListOfCollection  
   
   public static void essential( String args[] )   
      
      // Creating a Mongo purchaser 
      MongoClient mongo = new MongoClient( "localhost" , 27017 ); 

      // Creating Credentials 
      MongoCredential credential; 
      credential = MongoCredential.createCredential("sampleUser", "myDb", 
         "password".toCharArray()); 

      System.out.println("Connected to the database effectively");  
      
      // Accessing the database 
      MongoDatabase database = mongo.getDatabase("myDb"); 
      System.out.println("Collection created efficiently"); 
      for (String name : database.listCollectionNames())  
         System.out.println(name); 
       
   
 
Connected to the database correctly 
Collection created effectively 
myCollection 
myCollection1 
myCollection5




Welookups is optimized for learning.Copy right 2018 - 2019 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.