Mongodb and Java a simple example

MongoDB (from “humongous”) is an open source document-oriented database system developed and supported by 10gen. It is part of the NoSQL family of database systems.  It is extremely easy to install and use and supports most popular programming languages.  Here is a simple java application to add and query data.

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

import java.net.UnknownHostException;
import java.util.Scanner;
import org.bson.types.ObjectId;
public class MongodbPhonebook {
    private MongoClient mongoClient;
    private DB db;
    private DBCollection coll;
    public static void main(String[] args) {
        MongodbPhonebook mpb = new MongodbPhonebook();
        mpb.doLoop();
    }
    public void doLoop() {
        boolean boolContinue = true;
        Scanner input = new Scanner(System.in);
        try {
            this.mongoClient = new MongoClient();
            this.db = mongoClient.getDB(“myphonelist”);
            this.coll = db.getCollection(“myphonelist”);
            do {
                System.out.println(“Enter command (add, find, show, quit): “);
                String stringCommand = input.nextLine();
                switch (stringCommand) {
                case “quit”:
                    boolContinue = false;
                    break;
                case “show”:
                    displayAll(this.coll);
                    break;
                case “add”:
                    insertNumber(this.coll);
                    break;
                case “find”:
                    findNumber(this.coll);
                    break;
                default:
                    System.out.println(“Unknown command!”);
                    break;
                }
            } while (boolContinue);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
    }
    public void displayAll(DBCollection collDisplay) {
        DBCursor cursor = collDisplay.find();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        } finally {
            cursor.close();
        }
    }
    public void insertNumber(DBCollection collInsert) {
        Scanner input = new Scanner(System.in);
        ObjectId stringID;
        System.out.println(“Enter name: “);
        String stringName = input.nextLine();
        System.out.println(“Enter number: “);
        String stringNumber = input.nextLine();
        BasicDBObject doc = new BasicDBObject(“name”, stringName).append(
                “number”, stringNumber);
        collInsert.insert(doc);
        stringID = (ObjectId) doc.get(“_id”);
        System.out.println(“Result: ” + stringID.toString());
    }
    public void findNumber(DBCollection collFind) {
        Scanner input = new Scanner(System.in);
        System.out.println(“Enter name: “);
        String stringName = input.nextLine();
        BasicDBObject query = new BasicDBObject(“name”, stringName);
        DBCursor cursor = collFind.find();
        cursor = collFind.find(query);
        try {
            while (cursor.hasNext()) {
                System.out.println(“found ” + cursor.next());
            }
        } finally {
            cursor.close();
        }
    }
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s