Simple CRUD operations with Java and MongoDB — Part 2

Supun Dharmarathne
technodyne
Published in
4 min readSep 3, 2012

Previous post was hoe to do data insertion, and this is about how to do data updating and deleting. Just as the data insertion, the update also has four ways to do it

  • Simple updating (collection.update(new BasicDBObject().append(“meal”,”Egg”), newDocument);).
  • Using “$inc”
  • Using “$set”
  • Using “$set” and collection.update(new BasicDBObject().append(“type”,”starch”), updateQuery, false,true);

Lets move on how to do it.
First we need to insert some dummy data.

[sourcecode language=”java”]

package com.technodyne.core;

import java.net.UnknownHostException;

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

/**
* Java MongoDB : Update document
*
*/

public class UpdateCollection {

public static void insertDummyDocuments(DBCollection collection){

BasicDBObject document = new BasicDBObject();
document.put(“meal”,”rice”);
document.put(“type”, “starch”);
document.put(“cost”, 100);

BasicDBObject document2 = new BasicDBObject();
document2.put(“meal”, “Egg”);
document2.put(“type”, “protien”);
document2.put(“cost”, 50);

BasicDBObject document3 = new BasicDBObject();
document3.put(“meal”,”bread”);
document3.put(“type”,”starch”);
document3.put(“cost”,70);

collection.insert(document);
collection.insert(document2);
collection.insert(document3);
}
[/sourcecode]

Add methods to print data collection and remove data collection.

[sourcecode language=”java”]

public static void printAllDocuments(DBCollection collection){
DBCursor cursor = collection.find();
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}

public static void removeAllDocuments(DBCollection collection){
collection.remove(new BasicDBObject());
}
[/sourcecode]

Now update the data as following four ways

[sourcecode language=”java”]
public static void main(String[]args){
try{
Mongo mongo = new Mongo(“127.0.0.1”,27017);
DB db = mongo.getDB(“technodyne”);

DBCollection collection = db.getCollection(“myDummyDataCollection”);

System.out.println(“Testing 1..”);
insertDummyDocuments(collection);

BasicDBObject newDocument = new BasicDBObject();
newDocument.put(“meal”,”Egg”);
newDocument.put(“type”,”protien and fat”);
newDocument.put(“cost”, 60);

collection.update(new BasicDBObject().append(“meal”,”Egg”), newDocument);

printAllDocuments(collection);
removeAllDocuments(collection);

System.out.println(“Testing 2…”);
insertDummyDocuments(collection);

BasicDBObject newDocument2 = new BasicDBObject().append(“$inc”,
new BasicDBObject().append(“cost”,150));
collection.update(new BasicDBObject().append(“meal”,”Egg” ), newDocument2);

printAllDocuments(collection);
removeAllDocuments(collection);

System.out.println(“Testing 3…”);
insertDummyDocuments(collection);

BasicDBObject newDocument3 = new BasicDBObject().append(“$set”,
new BasicDBObject().append(“type”,”Cereal”));
collection.update(new BasicDBObject().append(“meal”,”rice”),newDocument3);

printAllDocuments(collection);
removeAllDocuments(collection);

System.out.println(“Testing 4…”);
insertDummyDocuments(collection);

BasicDBObject updateQuery = new BasicDBObject().append(“$set”,
new BasicDBObject().append(“cost”, 150));
collection.update(new BasicDBObject().append(“type”,”starch”), updateQuery, false,true);

printAllDocuments(collection);
removeAllDocuments(collection);

System.out.println(“DONE!”);

}
catch (UnknownHostException e) {
// TODO: handle exception
e.printStackTrace();
}
catch (MongoException e) {
// TODO: handle exception
e.printStackTrace();
}
}
[/sourcecode]

Here is the output :

Testing 1..
{ “_id” : { “$oid” : “504469a82eef32a68a4e078b”} , “meal” : “rice” , “type” : “starch” , “cost” : 100}
{ “_id” : { “$oid” : “504469a82eef32a68a4e078c”} , “meal” : “Egg” , “type” : “protien and fat” , “cost” : 60}
{ “_id” : { “$oid” : “504469a82eef32a68a4e078d”} , “meal” : “bread” , “type” : “starch” , “cost” : 70}
Testing 2…
{ “_id” : { “$oid” : “504469a82eef32a68a4e078e”} , “meal” : “rice” , “type” : “starch” , “cost” : 100}
{ “_id” : { “$oid” : “504469a82eef32a68a4e078f”} , “meal” : “Egg” , “type” : “protien” , “cost” : 200}
{ “_id” : { “$oid” : “504469a82eef32a68a4e0790”} , “meal” : “bread” , “type” : “starch” , “cost” : 70}
Testing 3…
{ “_id” : { “$oid” : “504469a82eef32a68a4e0791”} , “meal” : “rice” , “type” : “Cereal” , “cost” : 100}
{ “_id” : { “$oid” : “504469a82eef32a68a4e0792”} , “meal” : “Egg” , “type” : “protien” , “cost” : 50}
{ “_id” : { “$oid” : “504469a82eef32a68a4e0793”} , “meal” : “bread” , “type” : “starch” , “cost” : 70}
Testing 4…
{ “_id” : { “$oid” : “504469a82eef32a68a4e0794”} , “meal” : “rice” , “type” : “starch” , “cost” : 150}
{ “_id” : { “$oid” : “504469a82eef32a68a4e0795”} , “meal” : “Egg” , “type” : “protien” , “cost” : 50}
{ “_id” : { “$oid” : “504469a82eef32a68a4e0796”} , “meal” : “bread” , “type” : “starch” , “cost” : 150}
DONE!

Lets move to how to delete data from mongoDB collection.

First add some dummy data .

[sourcecode language=”java”]

package com.technodyne.core;

import java.util.List;
import java.net.UnknownHostException;
import java.util.ArrayList;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class DeleteCollection {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Mongo mongo = new Mongo(“127.0.0.1”,27017);
DB db = mongo.getDB(“technodyne”);

DBCollection collection = db.getCollection(“dummyCollection”);

for(int i=1;i<=10;i++){
collection.insert(new BasicDBObject().append(“number”,i));
}

System.out.println(“Before reomoving”);
DBCursor cursor = collection.find();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
[/sourcecode]

Then , remove the data using different ways, as follows.

[sourcecode language=”java”]
System.out.println(“Data inserted”);
//remove number = 1
DBObject doc = collection.findOne();
collection.remove(doc);

//remove number = 2
BasicDBObject document = new BasicDBObject();
document.put(“number”,2);
collection.remove(document);

//remove number = 3
collection.remove(new BasicDBObject().append(“number”,3));

//remove number >9
BasicDBObject query = new BasicDBObject();
query.put(“number”,new BasicDBObject(“$gt”,9));
collection.remove(query);

//remove number = 4 and 5
BasicDBObject query2 = new BasicDBObject();
List list = new ArrayList();
list.add(4);
list.add(5);
query2.put(“number”,new BasicDBObject(“$in”,list));

//remove all documents
DBCursor cursor3 = collection.find();
while (cursor3.hasNext()) {
collection.remove(cursor3.next());
}
//collection.remove(new BasicDBObject());

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

System.out.println(“Done”);

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

}[/sourcecode]

The out put is

Before reomoving

{ “_id” : { “$oid” : “504473352eefa858b32dc25b”} , “number” : 1}

{ “_id” : { “$oid” : “504473352eefa858b32dc25c”} , “number” : 2}

{ “_id” : { “$oid” : “504473352eefa858b32dc25d”} , “number” : 3}

{ “_id” : { “$oid” : “504473352eefa858b32dc25e”} , “number” : 4}

{ “_id” : { “$oid” : “504473352eefa858b32dc25f”} , “number” : 5}

{ “_id” : { “$oid” : “504473352eefa858b32dc260”} , “number” : 6}

{ “_id” : { “$oid” : “504473352eefa858b32dc261”} , “number” : 7}

{ “_id” : { “$oid” : “504473352eefa858b32dc262”} , “number” : 8}

{ “_id” : { “$oid” : “504473352eefa858b32dc263”} , “number” : 9}

{ “_id” : { “$oid” : “504473352eefa858b32dc264”} , “number” : 10}

Data inserted

{ “_id” : { “$oid” : “504473352eefa858b32dc25e”} , “number” : 4}

{ “_id” : { “$oid” : “504473352eefa858b32dc25f”} , “number” : 5}

{ “_id” : { “$oid” : “504473352eefa858b32dc260”} , “number” : 6}

{ “_id” : { “$oid” : “504473352eefa858b32dc261”} , “number” : 7}

{ “_id” : { “$oid” : “504473352eefa858b32dc262”} , “number” : 8}

{ “_id” : { “$oid” : “504473352eefa858b32dc263”} , “number” : 9}

Done

--

--