0% found this document useful (0 votes)
220 views3 pages

Assignment B4

The document describes a Java program that implements CRUD (create, read, update, delete) operations on a MongoDB database. The program connects to a MongoDB database called "mydb", creates a collection called "Stud", and allows the user to select from a menu to insert, select, update or delete documents in the collection. It demonstrates inserting two documents, selecting all documents, updating the names in two documents, removing one document, and outputs the results of each operation.

Uploaded by

etaitu7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views3 pages

Assignment B4

The document describes a Java program that implements CRUD (create, read, update, delete) operations on a MongoDB database. The program connects to a MongoDB database called "mydb", creates a collection called "Stud", and allows the user to select from a menu to insert, select, update or delete documents in the collection. It demonstrates inserting two documents, selecting all documents, updating the names in two documents, removing one document, and outputs the results of each operation.

Uploaded by

etaitu7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment B4

Problem Statement: Database Connectivity:


Write a program to implement MongoDB database connectivity with any front
end language to implement Database navigation operations (add, delete, edit
etc.)

Code:
import [Link];
import [Link].*;
public class conn
{
public static void main(String[] args)
{
Try
{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = [Link]( "mydb" );
DBCollectipackage conMongo1;
on coll = [Link]("Stud",null);
int ch = 0;
do
{
[Link]("Enter your choice: \n [Link] \[Link] \[Link] \[Link]");
Scanner s=new Scanner([Link]);
ch=[Link]();

switch (ch)
{
case 1:

BasicDBObject doc1 = new


BasicDBObject("age","1").append("name","Mona");
BasicDBObject doc2 = new
BasicDBObject("age","2").append("name","swati");
[Link](doc1);
[Link](doc2);
DBCursor cursor = [Link]();
[Link]("Inserted into db");
while([Link]())
{
[Link]([Link]());
}
break;
case 2:
DBCursor cursor3 = [Link]();
[Link]("Display Records");
while([Link]())
{
[Link]([Link]());
}
break;
case 3:
BasicDBObject newDocument = new BasicDBObject();
[Link]("$set", new BasicDBObject().append("name",
"Mohan"));
[Link]("$set", new BasicDBObject().append("name",
"rahul"));
BasicDBObject searchQuery = new BasicDBObject().append("name",
"Ashwini");
BasicDBObject searchQuery1 = new BasicDBObject().append("name",
"swati");

[Link](searchQuery, newDocument);
[Link](searchQuery1, newDocument);
DBCursor cursor2 = [Link]();
[Link]("Updated into db");

while([Link]())
{
[Link]([Link]());
}
break;
case 4:
[Link]("Data is removed");
BasicDBObject R1 = new BasicDBObject();
[Link]("name", "Mona");
[Link](R1);
BasicDBObject R2 = new BasicDBObject();
[Link]("name", "swati");
[Link](R2);
break;
default:
break;
}

} while (ch!=6);
}
catch(Exception e)
{
[Link]();
}
}
}

Output:
Enter your choice:
[Link]
[Link]
[Link]
[Link]
1
Inserted into db
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Ashwini"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "swati"}
Enter your choice:
[Link]
[Link]
[Link]
[Link]
2
Display Records
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Ashwini"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "swati"}
Enter your choice:
[Link]
[Link]
[Link]
[Link]
3
Updated into db
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Mohan"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "rahul"}
Enter your choice:
[Link]
[Link]
[Link]
[Link]
2
Display Records
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Mohan"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "rahul"}
Enter your choice:
[Link]
[Link]
[Link]
[Link]
4
Data is removed
Enter your choice:
[Link]
[Link]
[Link]
[Link]

You might also like