============ Simple MongoDB CRUD Operations ============
[hnpandya@localhost ~]$ su========= CRUD Operations Java Program =========
Password:
[root@localhost hnpandya]# service mongod start
Redirecting to /bin/systemctl start mongod.service
[root@localhost hnpandya]# mongo
MongoDB shell version: 2.4.6
connecting to: test
> show dbs; //Show database
local 0.078125GB
> use student; //create/use databases
switched to db student
> db //to check working database
student
> show dbs
local 0.078125GB
student 0.203125GB
> db.createCollection("stud"); //create collection
{ "ok" : 1 }
> db.stud.insert({Name: 'Hardik', Rno: 62, Branch: 'Computer' });
// to insert entries in collection
> db.stud.insert({Name: 'Pandya', Rno: 62, Branch: 'Computer' });
> db.stud.insert({Name: 'ABC', Rno: 1, Branch: 'IT' });
> db.stud.insert({Name: 'XYZ', Rno: 2, Branch: 'E&TC'});
> db.stud.find(); //display all records from collection
{ "_id" : ObjectId("543ba23710f5fed7ee1c59d2"), "Name" : "Hardik", "Rno" : 62, "Branch" : "Computer" }
{ "_id" : ObjectId("543ba24710f5fed7ee1c59d3"), "Name" : "Pandya", "Rno" : 62, "Branch" : "Computer" }
{ "_id" : ObjectId("543ba25f10f5fed7ee1c59d4"), "Name" : "ABC", "Rno" : 1, "Branch" : "IT" }
{ "_id" : ObjectId("543ba27810f5fed7ee1c59d5"), "Name" : "XYZ", "Rno" : 2, "Branch" : "E&TC" }
> db.stud.find().pretty(); // To display column wise Records Vertically
{
"_id" : ObjectId("543ba23710f5fed7ee1c59d2"),
"Name" : "Hardik",
"Rno" : 62,
"Branch" : "Computer"
}
{
"_id" : ObjectId("543ba24710f5fed7ee1c59d3"),
"Name" : "Pandya",
"Rno" : 62,
"Branch" : "Computer"
}
{
"_id" : ObjectId("543ba25f10f5fed7ee1c59d4"),
"Name" : "ABC",
"Rno" : 1,
"Branch" : "IT"
}
{
"_id" : ObjectId("543ba27810f5fed7ee1c59d5"),
"Name" : "XYZ",
"Rno" : 2,
"Branch" : "E&TC"
}
> db.movie.insert({"name":"Iron Man"}); //To create collection while inserting data
> show collections;
movie
stud
system.indexes
> db.movie.find();
{ "_id" : ObjectId("543ba2fd10f5fed7ee1c59d6"), "name" : "Iron Man" }
> db.movie.update({'name':'Iron Man'},{$set:{'name':'Iron Man 3'}})
//Update using same fields
> db.movie.find();
{ "_id" : ObjectId("543ba2fd10f5fed7ee1c59d6"), "name" : "Iron Man 3" }
> db.stud.update({'Name':'XYZ'},{$set:{'Branch':'IT'}})
//Update using different fields
> db.stud.find();
{ "_id" : ObjectId("543ba23710f5fed7ee1c59d2"), "Name" : "Hardik", "Rno" : 62, "Branch" : "Computer" }
{ "_id" : ObjectId("543ba24710f5fed7ee1c59d3"), "Name" : "Pandya", "Rno" : 62, "Branch" : "Computer" }
{ "_id" : ObjectId("543ba25f10f5fed7ee1c59d4"), "Name" : "ABC", "Rno" : 1, "Branch" : "IT" }
{ "Branch" : "IT", "Name" : "XYZ", "Rno" : 2, "_id" : ObjectId("543ba27810f5fed7ee1c59d5") }
> db.stud.find().pretty();
{
"_id" : ObjectId("543ba23710f5fed7ee1c59d2"),
"Name" : "Hardik",
"Rno" : 62,
"Branch" : "Computer"
}
{
"_id" : ObjectId("543ba24710f5fed7ee1c59d3"),
"Name" : "Pandya",
"Rno" : 62,
"Branch" : "Computer"
}
{
"_id" : ObjectId("543ba25f10f5fed7ee1c59d4"),
"Name" : "ABC",
"Rno" : 1,
"Branch" : "IT"
}
{
"Branch" : "IT",
"Name" : "XYZ",
"Rno" : 2,
"_id" : ObjectId("543ba27810f5fed7ee1c59d5")
}
> db.stud.remove({'Rno':2}) //To Remove Particular entry
> db.stud.find().pretty();
{
"_id" : ObjectId("543ba23710f5fed7ee1c59d2"),
"Name" : "Hardik",
"Rno" : 62,
"Branch" : "Computer"
}
{
"_id" : ObjectId("543ba24710f5fed7ee1c59d3"),
"Name" : "Pandya",
"Rno" : 62,
"Branch" : "Computer"
}
{
"_id" : ObjectId("543ba25f10f5fed7ee1c59d4"),
"Name" : "ABC",
"Rno" : 1,
"Branch" : "IT"
}
> db.stud.find();
{ "_id" : ObjectId("543ba23710f5fed7ee1c59d2"), "Name" : "Hardik", "Rno" : 62, "Branch" : "Computer" }
{ "_id" : ObjectId("543ba24710f5fed7ee1c59d3"), "Name" : "Pandya", "Rno" : 62, "Branch" : "Computer" }
{ "_id" : ObjectId("543ba25f10f5fed7ee1c59d4"), "Name" : "ABC", "Rno" : 1, "Branch" : "IT" }
> db.movie.drop() //To Drop Collections
true
> show collections
stud
system.indexes
> db.movie.find()
>
import java.net.UnknownHostException;
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 Crud {
public static void main(String[] args) throws UnknownHostException {
TestData.countriesTestData();
//------------------------------------------------- set up
MongoClient client = new MongoClient( "mongo-host" , 27017 );
DB db = client.getDB( "world" );
DBCollection countries = db.getCollection("countries");
//------------------------------------------------- create
BasicDBObject iceland = new BasicDBObject("name", "Iceland")
.append("area", 39770)
.append("population", 321857)
.append("continent", new BasicDBObject("name", "Europe"));
countries.insert(iceland);
//------------------------------------------------- read
DBObject firstCountry = countries.findOne();
System.out.println("First country name: " + firstCountry.get("name"));
BasicDBObject greeceQuery = new BasicDBObject("name", "Greece");
DBObject greece = countries.findOne(greeceQuery);
System.out.println("Area of Greece: " + greece.get("area"));
BasicDBObject europeQuery = new BasicDBObject("continent.name", "Europe");
DBCursor countryCursor = countries.find(europeQuery);
System.out.println("Countries in Europe");
try {
while(countryCursor.hasNext()) {
System.out.println(" " + countryCursor.next());
}
} finally {
countryCursor.close();
}
//------------------------------------------------- update
BasicDBObject georgiaQuery = new BasicDBObject("name", "Georgia");
BasicDBObject georgiaUpdate = new BasicDBObject();
georgiaUpdate.put("$set", new BasicDBObject("population", 4555911));
countries.update(georgiaQuery, georgiaUpdate);
System.out.println("Population of Georgia: " + countries.findOne(georgiaQuery).get("population"));
//------------------------------------------------- delete
BasicDBObject serbia = new BasicDBObject("name", "Serbia");
countries.remove(serbia);
System.out.println("Serbia exists: " + (countries.findOne(serbia) != null));
}
}