MongoDB + Java + Maven Sample Application


Prerequisite: Download and Install MongoDB (http://docs.mongodb.org/manual/installation/) on your local machine and run two instances in two separate terminal/command prompt. This sample app is developed on Mac OS/Linux env.

1. Start MongoDB server using : ./mongod

2. Start MongoDB client : ./mongo

Add MongoDB Jar dependency in your project:

 <dependency>
	<groupId>org.mongodb</groupId>
	<artifactId>mongo-java-driver</artifactId>
	<version>2.10.1</version>
 </dependency>

 

Sample MONGODB CRUD code:

package com.db;
import java.util.List;
import java.util.Set;

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

public class MongoConnectionManager {
	static DBCursor cursor;

	public static void main(String[] args) {

	try{
		System.out.println("Connecting with MongoDB......");
		//DB connection after ver 2.10.0
		MongoClient mongoconn = new MongoClient("localhost",27017 );
		//Get database
		DB db = mongoconn.getDB( "test" );
		System.out.println("Database Name: "+db.getName());

		//Display all databases
		List<String> dbs = mongoconn.getDatabaseNames();
		for(String mydb : dbs){
			System.out.println(mydb);
		}

		//If MongoDB is installed on Secure mode
		//boolean auth = db.authenticate("username", "password".toCharArray());

		//Get Collection/tables names
		Set<String> colls = db.getCollectionNames();

		for (String s : colls) {
		    System.out.println(s);
		}

		//Get Collection

		DBCollection coll = db.getCollection("mynames");
		coll.createIndex(new BasicDBObject("email", 1));  // create index on "i", ascending

		System.out.printf("Collection Names:"+db.getCollectionNames());

		//Drop collections
		coll.drop(); 

		//Insert new records/documents
		BasicDBObject doc1 = new BasicDBObject();

		//Insert values into document/record
        doc1.put("name", "Rajiv");
        doc1.put("email", "rajivmca2004@yahoo.com");
        doc1.put("count",1);

     // Insert values in Collection
        coll.insert(doc1);

        BasicDBObject doc2 = new BasicDBObject();

		//Insert values into document/record
        doc2.put("name", "Mohit");
        doc2.put("email", "mohit@yahoo.com");
        doc2.put("count",2);

       // Insert values in Collection
        coll.insert(doc2);

        //Fecth only first doc

        DBObject mydoc= coll.findOne();
        System.out.println("Only First Doc :"+mydoc);

        //Insert and fetch Multiple doc

        for (int i = 0; i < 10; i++) {
			coll.insert(new BasicDBObject().append("count", i));
		}

        //Total documents
       cursor= coll.find();

        while(cursor.hasNext()){
        	System.out.println(cursor.next());
        }

        //Getting A Single Document with A Query
        BasicDBObject query= new BasicDBObject();
        query.put("name","Rajiv");
        cursor= coll.find(query);
        System.out.print("Getting A Single Document with A Query: \n");

        //Iterate over database results
        while(cursor.hasNext()){
        	System.out.println(cursor.next());
        }

        //Conditional Queries, documents where i > 50. We could also get a range, say 20 < i <= 30 :
        query = new BasicDBObject();
        query.put("count", new BasicDBObject("$gt", 1).append("$lte", 30));  // i.e.   20 < i <= 30

        cursor = coll.find(query);

        while(cursor.hasNext()) {
            System.out.println("Comparison =>"+cursor.next());
        }
        System.out.println("Total documents: "+coll.getCount());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

Advertisements

Designing Modern Enterprise Search Solution –Part 1


This article would be a two part series.

  1. Part 1 – 5 requirements that will force you to adopt an enterprise search engine.
  2. Part 2– Addressing Enterprise search requirements with open source search platforms.

 

Part 1 – 5 requirements that will force you to adopt an enterprise search engine.

  1. Search over diverse content sources- An enterprise may need a search over diverse content sources including databases, document store, emails, JIRA, SharePoint and content management systems. A smart search solution should be capable enough to crawl these sources and make content searchable.
  2. Modern Search features- Now a days google has become benchmark for advance search features like Autosuggest, Did you mean “AAA”.
  3. Support powerful UI- To build a powerful search interface faceting (filters for search results) is must. I would be better if search engine provide responses in JSON so that
  4. Performance- For e.g. most of the searches below 100 ms.
  5. Full text search- With full text, search engine just do not match user specified term with 100% precision like “SQL like”. A full text search gives you flexibility to tune down the precision for better recall. Three full text realted concepta are  tokenization, stemming and ranking. Tokenization is Splitting a text into words, phrases and tokens. Generally it is done based on language grammar.