Sample Collection, Documents & MongoUtils.java

Below is the sample collection that we are using in our all the examples.

Documents :

{ "_id" : ObjectId("53559e2ca2f880fe942e76c4"), "firstName" : "rod", "lastName" : "johnson", "founder" : "Spring", "wiki" : "http://en.wikipedia.org/wiki/Rod_Johnson_%28programmer%29", "hits" : 45000}
{ "_id" : ObjectId("53559e2ca2f880fe942e76c5"), "firstName" : "kevin", "lastName" : "ryan", "founder" : "mongo", "wiki" : "http://en.wikipedia.org/wiki/Kevin_P._Ryan", "hits" : 32000 }
{ "_id" : ObjectId("53559e2ca2f880fe942e76c6"), "firstName" : "james", "lastName" : "gosling", "founder" : "java", "wiki" : "http://en.wikipedia.org/wiki/James_Gosling", "hits" : 98000 }
{ "_id" : ObjectId("53559e2ca2f880fe942e76c7"), "firstName" : "doug", "lastName" : "cutting", "founder" : "Hadoop", "wiki" : "http://en.wikipedia.org/wiki/Doug_Cutting", "hits" : 28000 }
{ "_id" : ObjectId("53559e2ca2f880fe942e76c8"), "firstName" : "eliot", "lastName" : "Horowitz", "founder" : "mongo", "wiki" : "http://en.wikipedia.org/wiki/Eliot_Horowitz", "hits" : 38000 }

This is the utility class that we are using in all the examples.
MongoUtils.java

package com.lotusmedia.mongo;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class MongoUtils {

	private static final String DATABASE_NAME="lotusDB";
	private static final String DEFAULT_COLLECTION_NAME="yoMongo";
	private static MongoClient serverConnection = null;
	
	//TODO Thread Safety
	public static DBCollection getCollection(String collectionName) {
		if(serverConnection==null){
			try {
				serverConnection = new MongoClient("localhost",27017);
			} catch (UnknownHostException e) {
				e.printStackTrace();
				throw new RuntimeException("Not able to connect to localhost");
			}
		}		
		DB db = serverConnection.getDB(DATABASE_NAME);
		return db.getCollection(collectionName==null?DEFAULT_COLLECTION_NAME:collectionName);
	}
	
	
	public static void printDocument(String key,String value){
		System.out.println(getCollection("yoMongo").findOne(new BasicDBObject().append(key, value)));
	}
	
	public static void printAllDocument(String key,String value){
		DBCursor cursor = getCollection("yoMongo").find(new BasicDBObject().append(key, value));
		while(cursor.hasNext()){
			System.out.println(cursor.next());
		}		
	}
	
	
	public static void dropAndRecreateCollection(String collectionName){
		DBCollection yoMongo=MongoUtils.getCollection(collectionName);
		yoMongo.drop();
		DBObject firstObject = BasicDBObjectBuilder.start().add("firstName", "rod").append("lastName", "johnson").
				append("founder", "Spring").append("wiki","http://en.wikipedia.org/wiki/Rod_Johnson_%28programmer%29").append("hits",45000).get();
		MongoUtils.getCollection("yoMongo").insert(firstObject);
		firstObject = BasicDBObjectBuilder.start().add("firstName", "kevin").append("lastName", "ryan").
				append("founder", "mongo").append("wiki","http://en.wikipedia.org/wiki/Kevin_P._Ryan").append("hits",32000).get();
		MongoUtils.getCollection("yoMongo").insert(firstObject);
		firstObject = BasicDBObjectBuilder.start().add("firstName", "james").
				append("lastName", "gosling").append("founder", "java").append("wiki","http://en.wikipedia.org/wiki/James_Gosling").append("hits",98000).get();
		MongoUtils.getCollection("yoMongo").insert(firstObject);
		firstObject = BasicDBObjectBuilder.start().add("firstName", "doug").
				append("lastName", "cutting").append("founder", "Spring").append("wiki","http://en.wikipedia.org/wiki/Doug_Cutting").append("hits",28000).get();
		MongoUtils.getCollection("yoMongo").insert(firstObject);
		firstObject = BasicDBObjectBuilder.start().add("firstName", "eliot").
				append("lastName", "Horowitz").append("founder", "mongo").append("wiki","http://en.wikipedia.org/wiki/Eliot_Horowitz").append("hits",38000).get();
		MongoUtils.getCollection("yoMongo").insert(firstObject);
	}
}

Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect

2 thoughts on “Sample Collection, Documents & MongoUtils.java

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>