tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 No SQL > Mongo DB > How to list all databases in Mongo Server

How to list all databases in Mongo Server 

MongoDB is a scalable, high-performance, open source NoSQL database. It offers Ad hoc queries, Indexing, Replication, Load balancing, File storage (GridFS), Aggregation, MapReduce and Server-side JavaScript execution. This requires the library mongo-2.8.0.jar to be present in the classpath. The following example shows how to get the names of all databases present on the Mongo Server.

File Name  :  
com/bethecoder/tutorials/mongodb/tests/ShowDatabasesTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.mongodb.tests;

import java.net.UnknownHostException;
import java.util.List;

import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class ShowDatabasesTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    try {
      //Connect to mongoDB with given IP and PORT
      Mongo mongo = new Mongo("localhost"27017);
      
      //Gets a list of all database names present on the server
      List<String> databaseNames = mongo.getDatabaseNames();

      for (String databaseName : databaseNames) {
        System.out.println(databaseName);
      }
      
    catch (UnknownHostException e) {
      e.printStackTrace();
    catch (MongoException e) {
      e.printStackTrace();
    }
  }
}
   

It gives the following output,
mytestdb
local



 
  


  
bl  br