Create a Maven project and add below dependency in pom.xml
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.1.1</version>
</dependency>Create a Java class to Delete data
package com.vinod.examples;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class MongoDeleteExample {
public static void main(String[] args) {
try {
System.out.println("MongodbDeleteDocument example...");
// Creating mongoinstance
Mongo mongo = new Mongo("localhost", 27017);
// Creating database instance
DB db = mongo.getDB("vinodDB");
// Creating collection object
DBCollection collection = db.getCollection("pretechUpdate1");
// BasicDBObject helpful create docuent object
// Creating contact object
BasicDBObject contact = new BasicDBObject();
contact.put("name", "vinod");
contact.put("address", "bangalore");
// Creating phone number object
BasicDBObject phoneNumber = new BasicDBObject();
phoneNumber.put("cell", "9999999999");
phoneNumber.put("landline", "08099999999");
phoneNumber.put("fax", "08099999990");
// adding phone number object in to contact object
contact.put("Phone numbers", phoneNumber);
// Inserting document
collection.insert(contact);
// Retrieving collection details
DBCursor cursorDoc = collection.find();
while (cursorDoc.hasNext()) {
System.out.println("Address details " + cursorDoc.next());
}
// Removing documents
collection.remove(new BasicDBObject().append("name", "vinod"));
// Retrieving collection details
DBCursor newcursorDoc = collection.find();
System.out.println("No of records exists" + newcursorDoc.count());
System.out.println("End of program");
} catch (MongoException e) {
e.printStackTrace();
}
}
}Output
Address details { "_id" : { "$oid" : "568e2548f5d3c83573a09bd1"} , "name" : "vinod" , "address" : "bangalore" ,
"Phone numbers" : { "cell" : "9999999999" , "landline" : "08099999999"
, "fax" : "08099999990"}}
No of records exists0
End of programDone !!!
Java Mongodb Delete Example
Subscribe to:
Post Comments (Atom)
Model Context Protocol (MCP) — Complete Guide for Backend Engineers
Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...
Featured Posts
-
In this example we will see how do to the file encryption and decryption using Apache Camel using pgp 1. Generate pgp keys In order to do t...
-
Spring provides a JMS integration framework that simplifies the use of the JMS API, the JmsTemplate class is the core class which is availab...
-
The selective consumer is consumer that applies a filter to incoming messages so that only messages meeting that specific selection criteria...
-
Apache camel API has the inbuilt kafka component and it is very simple to create producer, consumer and process messages. Here is one simple...
-
Apache camel provides intercepting feature while Exchanges are on route. Camel supports three types for interceptors ( See more about Camel ...
-
Maven Error Notes [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on projec...
-
In the previous example ( Mocking Static methods ) we created mock values for Static methods, in this example we will see how to mock new in...
-
Itext PDF is an open source API that allows to create and modify pdf documents in java. In this example we will see how to create and and i...
-
Camel Timer component is used to generate or process message exchanges when a time fires. Here is one example to print ‘Hello world ‘ in eac...
-
🔢 Java Sorting Algorithms — Step-by-Step with Iterations Sorting is a fundamental concept in programming and data structures. Below are t...
No comments:
Post a Comment