Home > Java, mongodb, NOSQL > Simple CRUD operations with Java and MongoDB – Part 1

Simple CRUD operations with Java and MongoDB – Part 1


This post is about how to do basic CRUD operations using java and MongoDB. 

There are 4 ways to insert document to MongoDB using java.
  • BasicDBObject
  • BasicDBObjectBuilder
  • Map (Using java HashMap)
  • Simple JSON string

Suppose here is the data we need to insert.

{
 "database" : "technodyne",
 "table" : "MyCollection",
 "detail" : 
  {
   records : 99,
   index : "vps_index1",
   active : "true"
  }
 }
}

Here is how to perform data insertion in four different ways.


package com.technodyne.core;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

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.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;

public class InsertDocumentApp {

public void insertDocument(){

 try {

 Mongo mongo = new Mongo("127.0.0.1",27017);
 DB db = mongo.getDB("technodyne");

 DBCollection collection = db.getCollection("MyCollection");

Using BasicDBObject 


// BasicDBObject example
 System.out.println("BasicDBObject example...");
 BasicDBObject document = new BasicDBObject();
 document.put("database", "technodyne");
 document.put("table", "MyCollection");

 BasicDBObject documentDetail = new BasicDBObject();
 documentDetail.put("records", "99");
 documentDetail.put("index", "vps_index1");
 documentDetail.put("active", "true");

 document.put("detail",documentDetail);

 collection.insert(document);

 DBCursor cursor = collection.find();
 while(cursor.hasNext()){
 System.out.println(cursor.next());
 }
 collection.remove(new BasicDBObject());

Using BasicDBObjectBuilder


// BasicDBObjectBuilder example
 System.out.println("BasicDBObjectBuilder example...");
 BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start().
 add("database", "technodyne")
 .add("table", "MyCollection");
 BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()
 .add("records", "99")
 .add("index","vps_index1")
 .add("active","true");
 documentBuilder.add("detail",documentBuilderDetail.get());

 collection.insert(documentBuilder.get());

 DBCursor cursorDocBuilder = collection.find();
 while(cursorDocBuilder.hasNext()){
 System.out.println(cursorDocBuilder.next());
 }
 collection.remove(new BasicDBObject());


Using hashMap


// Map example
 System.out.println("Map example...");
 Map documentMap = new HashMap();
 documentMap.put("database", "technodyne");
 documentMap.put("table", "MyCollection");

 Map documentMapDetail = new HashMap();
 documentMapDetail.put("records", "99");
 documentMapDetail.put("index","vps_index1");
 documentMapDetail.put("active", "true");

 documentMap.put("detail",documentMapDetail);

 collection.insert(new BasicDBObject(documentMap));

 DBCursor cursorDocMap = collection.find();
 while(cursorDocMap.hasNext()){
 System.out.println(cursorDocMap.next());
 }

 collection.remove(new BasicDBObject());


Finally json Object


// JSON parse example
 System.out.println("JSON parse example...");
 String json ="{'database':'technodyne','table':'MyCollection',"+
 "'detail':{'records':99,'index':'vps_index1','active':'true'}}";

 DBObject dbObject = (DBObject) JSON.parse(json);
 collection.insert(dbObject);

 DBCursor cursorDocJSON = collection.find();
 while (cursorDocJSON.hasNext()) {
 System.out.println(cursorDocJSON.next());

 }
 } catch (UnknownHostException e) {
 // TODO: handle exception
 e.printStackTrace();
 }

 catch (MongoException e) {
 // TODO: handle exception
 e.printStackTrace();
 }
 }
}

Then run this method

InsertDocumentApp insertApp = new InsertDocumentApp();
 insertApp.insertDocument();
This is the result
 
 
Advertisements
Categories: Java, mongodb, NOSQL
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: