13. MongoDB : Java program to Insert / Retrieve / Update / Delete the documents
*************************************************************
1> MongoClient mongoClient = new MongoClient("localhost", 27017);
2> MongoDatabase db = mongoClient.getDatabase("test");
3> MongoCollection<Document> collection = db.getCollection("java_item");
4> Document document = new Document("title", "MongoDB").append("description", "database").append("likes", 100).append("name","Pavan");
5> collection.insertOne(document);
***************************************************************
///////////////////////////////////////////INSERT ONE DOCUMENT #############################
package com.mongodb.createdocuments;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class InsertDocuments {
public static void main(String[] args) {
// To connect to the mongoDB server
MongoClient mongoClient = new MongoClient("localhost", 27017);
// Now connect to your database, in mogoDB 4.0, DB has changed to MongoDatabase
MongoDatabase db = mongoClient.getDatabase("test");
System.out.println("Connection to the database successful.");
MongoCollection<Document> collection = db.getCollection("java_item");
System.out.println("Collection item selected successfully.");
Document document = new Document("title", "MongoDB").append("description", "database").append("likes", 100).append("name","Pavan");
collection.insertOne(document);
System.out.println("Document inserted successfully.");
mongoClient.close();
}
}
#############################RETRIEVE DOCUMENTS####################################################
package com.mongodb.retrievedocuments;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
//import com.mongodb.client.MongoDatabase;
public class RetrievedDocuments {
public static void main(String[] args) {
// To connect to the mongoDB server
MongoClient mongoClient = new MongoClient("localhost", 27017);
// Now connect to your database
DB db = mongoClient.getDB("test");
//MongoDatabase db = mongoClient.getDatabase("test");
System.out.println("Connection to the database successful.");
DBCollection collection = db.getCollection("items");
System.out.println("Collection items selected successfully.");
DBCursor cursor = collection.find();
int index = 1;
while (cursor.hasNext()) {
System.out.println("Inserted document: " + index);
System.out.println(cursor.next());
index++;
}
System.out.println("Documents retrieved successfully.");
mongoClient.close();
}
}
#############################UPDATE DOCUMENTS####################################################
package com.mongodb.updateddouments;
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 UpdateDocuments {
public static void main(String[] args) {
try{
// To connect to the mongoDB server
MongoClient mongoClient = new MongoClient("localhost", 27017);
// Now connect to your database
DB db = mongoClient.getDB("test");
System.out.println("Connection to the database successful.");
DBCollection collection = db.getCollection("java_item");
DBCursor cursor = collection.find();
System.out.println("Collection items selected successfully.");
while(cursor.hasNext()){
DBObject updateDocument = cursor.next();
updateDocument.put("likes", 300);
BasicDBObject query = new BasicDBObject().append("title", "MongoDB");
collection.update(query, updateDocument, false, false);
}
System.out.println("Document updated successfully.");
cursor = collection.find();
int index=1;
while(cursor.hasNext()){
System.out.println("Updated Document: " + index);
System.out.println(cursor.next());
index++;
}
mongoClient.close();
}catch(Exception exception){
System.err.println(exception.getClass().getName() + ":" + exception.getMessage());
}
}
}
#############################DELETE DOCUMENTS####################################################
package com.mongodb.deletedocument;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
public class DeleteDocument {
public static void main(String[] args) throws InterruptedException {
// To connect to the mongoDB server
MongoClient mongoClient = new MongoClient("localhost", 27017);
// Now connect to your database
DB db = mongoClient.getDB("test");
System.out.println("Connection to the database successful.");
DBCollection collection = db.getCollection("java_item");
System.out.println("Collection items selected successfully.");
BasicDBObject searchQuery = new BasicDBObject().append("title", "MongoDB");
collection.remove(searchQuery);
DBCursor cursor = collection.find();
int index = 1;
while (cursor.hasNext()) {
System.out.println("Inserted document: " + index);
System.out.println(cursor.next());
index++;
}
System.out.println("Documents retrieved successfully.");
mongoClient.close();
}
}
No comments:
Post a Comment