DBMS 14 - Theory+Code
DBMS 14 - Theory+Code
Problem Statement:
Write a program to implement MongoDB database connectivity with any front
end language toimplement Database navigation operations (add, delete,
edit etc.)
# Hardware Requirement:
2GHz dual processor, 2GB RAM .
#Software Requirement:
Ubuntu os,Database MongoDB,Eclipse.
#THEORY
Create a Collection
To create a collection, createCollection() method of
com.mongodb.client.MongoDatabase class is used.
Getting/Selecting a Collection
Insert a Document
To get/select a collection from the database, getCollection() method of
com.mongodb.client.MongoDatabase class is used.
Insert a Document
To insert a document into MongoDB, insert() method of
com.mongodb.client.MongoCollection class is used.
Retrieve All Documents
To select all documents from the collection, find() method of
com.mongodb.client.MongoCollection class is used. This method returns a
cursor, so you need to iterate this cursor.
Update Document
To update a document from the collection, updateOne() method of
com.mongodb.client.MongoCollection class is used.
Delete a Document
To delete a document from the collection, you need to use the deleteOne()
method of the com.mongodb.client.MongoCollection class.
Dropping a Collection
To drop a collection from a database, you need to use the drop() method
of the com.mongodb.client.MongoCollection class.
Listing All the Collections
To list all the collections in a database, you need to use the
listCollectionNames() method of the com.mongodb.client.MongoDatabase
class.
#EXECUTION STEPS:
//import com.mongodb.*;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import java.util.Set;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class MongoConn {
public static void main(String args[]) throws Exception {
MongoClient mongoClient = new MongoClient();
DB db = mongoClient.getDB("shop");
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
// Get Single Collection
DBCollection coll = db.getCollection("shop");
// Inserting Document
BasicDBObject doc = new BasicDBObject("name", "MongoDB")
.append("type", "database").append("count", 1)
.append("info", new BasicDBObject("x", 203).append("y", 102));
coll.insert(doc);
// Finding First Document
DBObject myDoc = coll.findOne();
System.out.println(myDoc);
// Add Multiple Document
//for (int i = 0; i < 100; i++) {
// coll.insert(new BasicDBObject("i", i));
//}
// Count of documents in a collections
System.out.println(coll.getCount());
// Cursor to get all documents
DBCursor cursor = coll.find();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
}
}
#Output:
Oct 21, 2024 10:16:21 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:3}] to
127.0.0.1:27017
cloths
shop
studentinfo
{"_id": {"$oid": "6704c89d9ebeb075eb7e02a1"}, "name": "MongoDB", "type":
"database", "count": 1, "info": {"x": 203, "y": 102}}
9
{"_id": {"$oid": "6704c89d9ebeb075eb7e02a1"}, "name": "MongoDB", "type":
"database", "count": 1, "info": {"x": 203, "y": 102}}
{"_id": {"$oid": "6710a852f98c310434bdc9a0"}, "name": "casual", "price":
"1000"}
{"_id": {"$oid": "6710a8a4df3d5e1728e6d7ef"}, "name": "MongoDB", "type":
"database", "count": 1, "info": {"x": 203, "y": 102}}
{"_id": {"$oid": "6710acbe56a86a57f42d872d"}, "name": "MongoDB", "type":
"database", "count": 1, "info": {"x": 203, "y": 102}}
{"_id": {"$oid": "6710d24ef98c310434bdc9a1"}, "name": "partywear",
"price": "5000"}
{"_id": {"$oid": "6710d36cd8a5027382d5f581"}, "name": "MongoDB", "type":
"database", "count": 1, "info": {"x": 203, "y": 102}}
{"_id": {"$oid": "6710d4574235234010f32920"}, "name": "MongoDB", "type":
"database", "count": 1, "info": {"x": 203, "y": 102}}
{"_id": {"$oid": "6710d967fe04cc1f694f0975"}, "name": "MongoDB", "type":
"database", "count": 1, "info": {"x": 203, "y": 102}}
{"_id": {"$oid": "6715dc9da1fa8f46d659eac8"}, "name": "MongoDB", "type":
"database", "count": 1, "info": {"x": 203, "y": 102}}