tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 No SQL > Mongo DB > How to print MongoDB Collection statistics

How to print MongoDB Collection statistics 

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 statistics of a Collection.

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

import java.net.UnknownHostException;

import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class CollectionStatsTest {

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

    try {
      //Connect to mongoDB with given IP and PORT
      Mongo mongo = new Mongo("localhost"27017);
      
      //Get the database object
      DB database = mongo.getDB("mytestdb");
      
      //Gets a collection with a given name.
      DBCollection collection = database.getCollection("students");
    
      //Get the collections statistics
      CommandResult stats = collection.getStats();
      System.out.println(stats);

    catch (UnknownHostException e) {
      e.printStackTrace();
    catch (MongoException e) {
      e.printStackTrace();
    }
  }
}
   

It gives the following output,
{ 
	"serverUsed" : "localhost/127.0.0.1:27017" , 
	"ns" : "mytestdb.students" , 
	"count" : 2 , 
	"size" : 120 , 
	"avgObjSize" : 60.0 , 
	"storageSize" : 4096 , 
	"numExtents" : 1 , 
	"nindexes" : 1 , 
	"lastExtentSize" : 4096 , 
	"paddingFactor" : 1.0 , 
	"flags" : 1 , 
	"totalIndexSize" : 8176 , 
	"indexSizes" : { "_id_" : 8176} , 
	"ok" : 1.0
}



 
  


  
bl  br