You are here : Home / Core Java Tutorials / MongoDB Tutorial / MongoDB java tutorial
Contents of page >
1) Program to Delete Document From Collection In MongoDb in java
package deleteDocumentFromCollectionInMongoDb;
/**
* Write a program to Delete Document From Collection In MongoDb in java
*/
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
public class DeleteDocumentFromCollectionInMongoDb1_specificDocument {
public static void main(String[] args) {
/* Connect to MongoDB */
MongoClient mongoClient =
new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
/* Connect to DATABASE */
//If in case database doesn't exists, it will be created at runtime
DB db = mongoClient.getDB("mydb");
/* Get the COLLECTION (TABLE) */
//If in case collection (Table) doesn't exists, it will be created at runtime
DBCollection dbCollection = db.getCollection("Employee");
/*If collection already exists, you may remove everything
from collection for perfect OUTPUT of program*/
//dbCollection.remove(new BasicDBObject()); //Not Mandatory
/* create a BasicDBObject1 (document /record) */
BasicDBObject basicDBObject1 = new BasicDBObject();
basicDBObject1.put("id", 1);
basicDBObject1.put("name", "Ankit");
/* Insert document1/record1 in COLLECTION in MongoDB */
dbCollection.insert(basicDBObject1);
/* create a BasicDBObject2 (document /record) */
BasicDBObject basicDBObject2 = new BasicDBObject();
basicDBObject2.put("id", 2);
basicDBObject2.put("name", "Sam");
/* Insert document2/record2 in collection in MongoDB */
dbCollection.insert(basicDBObject2);
/* Display documents of collection before DELETE in MongoDB */
System.out.println("Display documents of collection before DELETE in MongoDB");
DBCursor cursor0 = dbCollection.find();
while (cursor0.hasNext()) {
System.out.println(cursor0.next());
}
/** At this stage Employee collection looks like this >
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8144"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8145"} , "id" : 2 , "name" : "Sam"}
*/
/** Now, let's DELETE the document */
/** DELETE document where id = 1 */
BasicDBObject basicDBObject_SearchCondition = new BasicDBObject();
basicDBObject_SearchCondition.put("id", 1);
/** Execute DELETE/REMOVE query - To DELETE DOCUMENT in MongoDB */
//remove method delete documents from a collection.
WriteResult deleteResult =
dbCollection.remove(basicDBObject_SearchCondition);
System.out.println("\nExecute DELETE/REMOVE query where id = 1");
System.out.println("No of documents deleted = " + deleteResult.getN());
/* Display documents of collection after DELETING DOCUMENT in MongoDB */
System.out.println("\nDisplay documents of collection after DELETING DOCUMENT in MongoDB");
DBCursor cursor = dbCollection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
/** At this stage Employee collection looks like this >
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8144"} , "id" : 1 , "name" : "Ankit_UPDATED"}
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8145"} , "id" : 2 , "name" : "Ankit_UPDATED"}
*/
}
}
/* OUTPUT
Display documents of collection before DELETE in MongoDB
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8144"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8145"} , "id" : 2 , "name" : "Sam"}
Execute DELETE/REMOVE query where id = 1
No of documents deleted = 1
Display documents of collection after DELETING DOCUMENT in MongoDB
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8145"} , "id" : 2 , "name" : "Sam"}
*/
|
Download >
Read : How to Import Maven project in eclipse
OR
2) Program to Delete Document From Collection In MongoDb in java on Basis Of Multiple Conditions >
package deleteDocumentFromCollectionInMongoDb;
/**
* Write a program to Delete Document From Collection In MongoDb in java on Basis Of Multiple Conditions
*/
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
public class DeleteDocumentFromCollectionInMongoDb2_onBasisOfMultipleConditions {
public static void main(String[] args) {
/* Connect to MongoDB */
MongoClient mongoClient =
new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
/* Connect to DATABASE */
//If in case database doesn't exists, it will be created at runtime
DB db = mongoClient.getDB("mydb");
/* Get the COLLECTION (TABLE) */
//If in case collection (Table) doesn't exists, it will be created at runtime
DBCollection dbCollection = db.getCollection("Employee");
/*If collection already exists, you may remove everything
from collection for perfect OUTPUT of program*/
//dbCollection.remove(new BasicDBObject()); //Not Mandatory
/* create a BasicDBObject1 (document /record) */
BasicDBObject basicDBObject1 = new BasicDBObject();
basicDBObject1.put("id", 1);
basicDBObject1.put("name", "Ankit");
basicDBObject1.put("salary", 100);
/* Insert document1/record1 in collection in MongoDB */
dbCollection.insert(basicDBObject1);
/* create a BasicDBObject2 (document /record) */
BasicDBObject basicDBObject2 = new BasicDBObject();
basicDBObject2.put("id", 2);
basicDBObject2.put("name", "Ankit");
basicDBObject2.put("salary", 200);
/* Insert document2/record2 in collection in MongoDB */
dbCollection.insert(basicDBObject2);
/* Display documents of collection before DELETE in MongoDB */
System.out.println("Display documents of collection before DELETE in MongoDB");
DBCursor cursor0 = dbCollection.find();
while (cursor0.hasNext()) {
System.out.println(cursor0.next());
}
/** At this stage Employee collection looks like this >
{ "_id" : { "$oid" : "58558a94ad8dc837cc7d94ae"} , "id" : 1 , "name" : "Ankit" , "salary" : 100}
{ "_id" : { "$oid" : "58558a94ad8dc837cc7d94af"} , "id" : 2 , "name" : "Ankit" , "salary" : 200}
*/
/** Now, let's DELETE the document */
/** DELETE document whose
* id = 1 and
* name = "Ankit"
*/
BasicDBObject basicDBObject_SearchCondition = new BasicDBObject();
basicDBObject_SearchCondition.append("id", 1).append("name", "Ankit");;
/** Execute DELETE/REMOVE query - To DELETE DOCUMENT in MongoDB */
//remove method delete documents from a collection.
WriteResult deleteResult =
dbCollection.remove(basicDBObject_SearchCondition);
System.out.println("\nExecute DELETE/REMOVE query where id = 1 and name = 'Ankit'");
System.out.println("No of documents deleted = " + deleteResult.getN());
/* Display documents of collection after DELETING DOCUMENT in MongoDB */
System.out.println("\nDisplay documents of collection after DELETING DOCUMENT in MongoDB");
DBCursor cursor = dbCollection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
/** At this stage Employee collection looks like this >
{ "_id" : { "$oid" : "58558a94ad8dc837cc7d94af"} , "id" : 2 , "name" : "Ankit" , "salary" : 200}
*/
}
}
/* OUTPUT
Display documents of collection before DELETE in MongoDB
{ "_id" : { "$oid" : "58558a94ad8dc837cc7d94ae"} , "id" : 1 , "name" : "Ankit" , "salary" : 100}
{ "_id" : { "$oid" : "58558a94ad8dc837cc7d94af"} , "id" : 2 , "name" : "Ankit" , "salary" : 200}
Execute DELETE/REMOVE query where id = 1 and name = 'Ankit'
No of documents deleted = 1
Display documents of collection after DELETING DOCUMENT in MongoDB
{ "_id" : { "$oid" : "58558a94ad8dc837cc7d94af"} , "id" : 2 , "name" : "Ankit" , "salary" : 200}
*/
|
3) Program to Delete all Documents From Collection In MongoDb in java >
package deleteDocumentFromCollectionInMongoDb;
/**
* Write a program to Delete all Documents From Collection In MongoDb in java
*/
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
public class DeleteDocumentFromCollectionInMongoDb3_AllDocumentsInCollection {
public static void main(String[] args) {
/* Connect to MongoDB */
MongoClient mongoClient =
new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
/* Connect to DATABASE */
//If in case database doesn't exists, it will be created at runtime
DB db = mongoClient.getDB("mydb");
/* Get the COLLECTION (TABLE) */
//If in case collection (Table) doesn't exists, it will be created at runtime
DBCollection dbCollection = db.getCollection("Employee");
/*If collection already exists, you may remove everything
from collection for perfect OUTPUT of program*/
//dbCollection.remove(new BasicDBObject()); //Not Mandatory
/* create a BasicDBObject (document /record) */
BasicDBObject basicDBObject1 = new BasicDBObject();
basicDBObject1.put("id", 1);
basicDBObject1.put("name", "Ankit");
/* Insert document/record in COLLECTION in MongoDB */
dbCollection.insert(basicDBObject1);
/* create a BasicDBObject (document /record) */
BasicDBObject basicDBObject2 = new BasicDBObject();
basicDBObject2.put("id", 2);
basicDBObject2.put("name", "Sam");
/* Insert document/record in collection in MongoDB */
dbCollection.insert(basicDBObject2);
/* Display documents of collection before DELETE in MongoDB */
System.out.println("Display documents of collection before DELETE in MongoDB");
DBCursor cursor0 = dbCollection.find();
while (cursor0.hasNext()) {
System.out.println(cursor0.next());
}
/** At this stage Employee collection looks like this >
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8144"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8145"} , "id" : 2 , "name" : "Sam"}
*/
/** Now, let's DELETE All Documents In Collection */
/** Execute DELETE/REMOVE query - To DELETE ALL DOCUMENTs in collection in MongoDB */
//remove method delete documents from a collection.
WriteResult deleteResult =
dbCollection.remove(new BasicDBObject());
System.out.println("\nExecute DELETE/REMOVE query- To DELETE ALL DOCUMENTs in collection");
System.out.println("No of documents deleted = " + deleteResult.getN());
System.out.println("All documents have been DELETED");
}
}
/* OUTPUT
Display documents of collection before DELETE in MongoDB
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8144"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8145"} , "id" : 2 , "name" : "Sam"}
Execute DELETE/REMOVE query- To DELETE ALL DOCUMENTs in collection
No of documents deleted = 2
All documents have been DELETED
*/
|
4) Program to Delete multiple Documents From Collection In MongoDb in java using In Operator >
package deleteDocumentFromCollectionInMongoDb;
/**
* Write a program to Delete multiple Documents From Collection In MongoDb in java Using In Operator
*/
import java.util.ArrayList;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
public class DeleteDocumentFromCollectionInMongoDb5_multipleDocumentsUsingInOperator {
public static void main(String[] args) {
/* Connect to MongoDB */
MongoClient mongoClient =
new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
/* Connect to DATABASE */
//If in case database doesn't exists, it will be created at runtime
DB db = mongoClient.getDB("mydb");
/* Get the COLLECTION (TABLE) */
//If in case collection (Table) doesn't exists, it will be created at runtime
DBCollection dbCollection = db.getCollection("Employee");
/*If collection already exists, you may remove everything
from collection for perfect OUTPUT of program*/
//dbCollection.remove(new BasicDBObject()); //Not Mandatory
/* create a BasicDBObject1 (document /record) */
BasicDBObject basicDBObject1 = new BasicDBObject();
basicDBObject1.put("id", 1);
basicDBObject1.put("name", "Ankit");
/* Insert document1/record1 in COLLECTION in MongoDB */
dbCollection.insert(basicDBObject1);
/* create a BasicDBObject2 (document /record) */
BasicDBObject basicDBObject2 = new BasicDBObject();
basicDBObject2.put("id", 2);
basicDBObject2.put("name", "Sam");
/* Insert document2 in collection in MongoDB */
dbCollection.insert(basicDBObject2);
/* create a BasicDBObject3 (document /record) */
BasicDBObject basicDBObject3 = new BasicDBObject();
basicDBObject3.put("id", 3); //Id is the column name, 1 is the value of column
basicDBObject3.put("name", "Neh");
/* Insert document3 in collection in MongoDB */
dbCollection.insert(basicDBObject3);
/* Display documents of collection before DELETING DOCUMENTs in MongoDB */
System.out.println("Display documents of collection before DELETE in MongoDB");
DBCursor cursor0 = dbCollection.find();
while (cursor0.hasNext()) {
System.out.println(cursor0.next());
}
/** At this stage Employee collection looks like this >
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3e"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3f"} , "id" : 2 , "name" : "Sam"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a40"} , "id" : 3 , "name" : "Neh"}
*/
/** Now, let's DELETE the multiple documents from collection in MongoDB */
//Create list of id's which you want to delete
List<Integer> idList = new ArrayList<Integer>();
idList.add(1);
idList.add(2);
/** DELETE document whose id = 1 and id = 2*/
BasicDBObject basicDBObject_SearchCondition = new BasicDBObject();
//Pass > $in as key > and > idList as value
basicDBObject_SearchCondition.put("id",new BasicDBObject("$in", idList));
/** Execute DELETE/REMOVE query - To DELETE DOCUMENT in MongoDB */
//remove method delete documents from a collection.
WriteResult deleteResult =
dbCollection.remove(basicDBObject_SearchCondition);
System.out.println("\nExecute DELETE/REMOVE query where id = 1 and id = 2");
System.out.println("No of documents deleted = " + deleteResult.getN());
/* Display documents of collection after DELETING DOCUMENTs in MongoDB */
System.out.println("\nDisplay documents of collection after DELETING DOCUMENTs in MongoDB");
DBCursor cursor = dbCollection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
/** At this stage Employee collection looks like this >
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a40"} , "id" : 3 , "name" : "Neh"}
*/
}
}
/* OUTPUT
Display documents of collection before DELETE in MongoDB
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3e"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a3f"} , "id" : 2 , "name" : "Sam"}
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a40"} , "id" : 3 , "name" : "Neh"}
Execute DELETE/REMOVE query where id = 1 and id = 2
No of documents deleted = 2
Display documents of collection after DELETING DOCUMENTs in MongoDB
{ "_id" : { "$oid" : "58559179ad8dc831e8df7a40"} , "id" : 3 , "name" : "Neh"}
*/
|
5) Program to Drop Collection In MongoDb in java >
package deleteDocumentFromCollectionInMongoDb;
/**
* Write a program to Drop Collection In MongoDb in java
*/
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class DropCollectionInMongoDb4 {
public static void main(String[] args) {
/* Connect to MongoDB */
MongoClient mongoClient =
new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
/* Connect to DATABASE */
//If in case database doesn't exists, it will be created at runtime
DB db = mongoClient.getDB("mydb");
/* Get the COLLECTION (TABLE) */
//If in case collection (Table) doesn't exists, it will be created at runtime
DBCollection dbCollection = db.getCollection("Employee");
/** Drop collection in MongoDB */
//drop method drops/deletes collection from the database
dbCollection.drop();
System.out.println("DROP collection from MongoDb");
System.out.println("Collection has been DROPPED");
}
}
/* OUTPUT
DROP collection from MongoDb
Collection has been DROPPED
*/
|
6) Deleting one document in collection in MongoDB in java>
//findOne method returns a single document from collection.
DBObject dbObject = dbCollection.findOne();
//Now, delete that one document which was returned
dbCollection.remove(dbObject);
|
Download >
Read : How to Import Maven project in eclipse
OR
Summary -
So in this MongoDB tutorial we learned with example and program how to to Delete Document In Collection In MongoDb in java.
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.
RELATED LINKS>