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.
[sourcecode language=”java”]
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”);
[/sourcecode]
Using BasicDBObject
[sourcecode language=”java”]
// 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());
[/sourcecode]
Using BasicDBObjectBuilder
[sourcecode language=”java”]
// 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());
[/sourcecode]
Using hashMap
[sourcecode language=”java”]
// 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());
[/sourcecode]
Finally json Object
[sourcecode language=”java”]
// 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();
}
}
}
[/sourcecode]
Then run this method
[sourcecode language=”java”]
InsertDocumentApp insertApp = new InsertDocumentApp();
insertApp.insertDocument();
[/sourcecode]
This is the result