<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">Of the four basic database operations (i.e. CRUD), delete operations are those that remove documents from a collection in MongoDB. In Java MongoDB API, you can use collection.remove() to delete document from collection. </p>
<p>If we want to remove the document from the collection &#8217;employees&#8217; which contains the empId &#8220;10001&#8221; the following mongodb command can be used : </p>
<pre class="highlight" name="code">>;db.employees.remove( { "empId" : "10001" } )
</pre>
<p>
Use the remove() method to delete documents from a collection. The remove() method has the following syntax:</p>
<pre class="highlight" name="code">db.collection.remove( <;query>;, <;justOne>; )
</pre>
<p>Corresponding operation in SQL<br />
The remove() method is analogous to the DELETE statement, and:</p>
<p>the <;query>; argument corresponds to the WHERE statement, and<br />
the <;justOne>; argument takes a Boolean and has the same affect as LIMIT 1.</p>
<p>remove() deletes documents from the collection. If you do not specify a query, remove() removes all documents from a collection, but does not remove the indexes.</p>
<div align='center' id="ads-id"></div>
<p>If we want to remove all data from the collection &#8217;employees&#8217; the following mongodb command can be used :</p>
<pre class="highlight" name="code">>;db.employees.remove( { } )
</pre>
<p>For testing the Remove function first we insert 10 documents to employees collection</p>
<pre class="highlight" name="code">for (int i=1001; i <;= 10010; i++) {
 collection.insert(new BasicDBObject().append("empId", i));
}
</pre>
<p>DBCollection.remove()- <b>1. Get first document and delete it. In this case, empId = 10001 is deleted.</b> </p>
<pre class="highlight" name="code">DBObject doc = collection.findOne(); //get first document
collection.remove(doc);
</pre>
<p><b>2. Puts query data in a BasicDBObject object. In this case, empId = 10002 is deleted.</b> </p>
<pre class="highlight" name="code">BasicDBObject document = new BasicDBObject();
document.put("empId", 10002);
collection.remove(document);
</pre>
<p><i><b>Query like this only delete empId= 3.</b></i> </p>
<pre class="highlight" name="code">BasicDBObject document = new BasicDBObject();
 document.put("empId", 10002);
 document.put("empId", 10003); //override above value 10002
 collection.remove(document);
</pre>
<p><i><b>Nice try, but query like this will not work, it will delete NOTHING.</b></i> </p>
<pre class="highlight" name="code">BasicDBObject document = new BasicDBObject();
 List<;Integer>; list = new ArrayList<;Integer>;();
 list.add(10007);
 list.add(10008);
 document.put("empId", list);
 collection.remove(document);
</pre>
<p><b>3. Use BasicDBObject directly. In this case, empId = 10002 is deleted.</b> </p>
<pre class="highlight" name="code">collection.remove(new BasicDBObject().append("empId", 10002));
</pre>
<p><b>4. Puts a &#8220;greater than&#8221; operator in a BasicDBObject object. In this case, empId = 10003 is deleted.</b> </p>
<pre class="highlight" name="code">BasicDBObject query = new BasicDBObject();
query.put("empId", new BasicDBObject("$gt", 10002));
collection.remove(query);
</pre>
</div>
<p><b>5. Puts a &#8220;in&#8221; operator in a BasicDBObject object, construct the query in ArrayList. In this case, empId = 10004 and empId = 10005 are deleted. </b></p>
<pre class="highlight" name="code">BasicDBObject query = new BasicDBObject();
List<;Integer>; list = new ArrayList<;Integer>;();
list.add(10004);
list.add(10005);
query.put("number", new BasicDBObject("$in", list));
collection.remove(query);
</pre>
<p><b>6. Use cursor to delete all available document. </b></p>
<pre class="highlight" name="code">DBCursor cursor = collection.find();
while (cursor.hasNext()) {
 collection.remove(cursor.next());
}
</pre>
<p><b>7. Pass an empty BasicDBObject, and cause it delete all available document. </b></p>
<pre class="highlight" name="code">collection.remove(new BasicDBObject());
</pre>
<p><a href="http://docs.mongodb.org/manual/reference/operators/#AdvancedQueries-ConditionalOperators" target="_blank"><b>More Conditional Operators-</b></a><br />
<b>$all-</b><br />
$all selects the documents where the field holds an array and contains all elements (e.g. <;value>;, <;value1>;, etc.) in the array. Consider the following example: </p>
<pre class="highlight" name="code">db.inventory.find( { tags: { $all: [ "appliances", "school", "book" ] } } )
</pre>
<p><b>$lt- ;</b><br />
Syntax: {field: {$lt: value} } $lt selects the documents where the value of the field is less than (i.e. <;) the specified value. Consider the following example: </p>
<pre class="highlight" name="code">db.inventory.find( { qty: { $lt: 20 } } )
</pre>
<p>This query will select all documents in the inventory collection where the qty field value is less than 20.</p>
<p><b>$gt-</b><br />
Syntax: {field: {$gt: value} }<br />
$gt selects those documents where the value of the field is greater than (i.e. >;) the specified value.</p>
<p>Consider the following example:</p>
<pre class="highlight" name="code">db.inventory.find( { qty: { $gt: 20 } } )
</pre>
<p>This query will select all documents in the inventory collection where the qty field value is greater than 20.</p></div>
<p>Now lets see the full example on the following java file and its output.</p>
<pre class="highlight" name="code">package com.dineshonjava.mongo.test;

import java.net.UnknownHostException;
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.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * @author Dinesh Rajput
 *
 */
public class DeleteDocumentDemo {

 /**
 * @param args
 */
 public static void main(String[] args) {
 try {
 // connect to mongoDB, IP and port number
 Mongo mongo = new Mongo("localhost", 27017);
 
 // get database from MongoDB,
 // if database doesn't exists, mongoDB will create it automatically
 DB db = mongo.getDB("dineshonjavaDB");
 
 // get a single collection
 DBCollection collection = db.getCollection("employee");
 
 //insert empId 10001 to 10010 for testing
 for (int i=10001; i <;= 10010; i++) {
 collection.insert(new BasicDBObject().append("empId", i));
 }
 
 //remove empId = 10001
 DBObject doc = collection.findOne(); //get first document
 collection.remove(doc);
 
 //remove empId = 10002
 BasicDBObject document = new BasicDBObject();
 document.put("empId", 10002);
 collection.remove(document);
 
 //remove empId = 10003
 collection.remove(new BasicDBObject().append("empId", 10003));
 
 //remove empId >; 10009 , means delete empId = 10010
 BasicDBObject query = new BasicDBObject();
 query.put("empId", new BasicDBObject("$gt", 10009));
 collection.remove(query);
 
 //remove empId = 10004 and 10005
 BasicDBObject query2 = new BasicDBObject();
 List<;Integer>; list = new ArrayList<;Integer>;();
 list.add(10004);
 list.add(10005);
 query2.put("empId", new BasicDBObject("$in", list));
 collection.remove(query2);
 
 //remove all documents
 //DBCursor cursor = collection.find();
 //while (cursor.hasNext()) {
 // collection.remove(cursor.next());
 //}
 
 //remove all documents , no query means delete all
 //collection.remove(new BasicDBObject());
 
 //print out the document
 DBCursor cursor = collection.find();
 while(cursor.hasNext()) {
 System.out.println(cursor.next());
 }
 
 System.out.println("Done");
 
 } catch (UnknownHostException e) {
 e.printStackTrace();
 } catch (MongoException e) {
 e.printStackTrace();
 }
 
 }

}
</pre>
<p>If everything is fine then run as a java application and you will get the following output on the console.</p>
<div style="background-color: #ff99cc;"><b>output:</b><br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;51054d998217798d9fc43545&#8221;} , &#8220;empId&#8221; : 10006}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;51054d998217798d9fc43546&#8221;} , &#8220;empId&#8221; : 10007}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;51054d998217798d9fc43547&#8221;} , &#8220;empId&#8221; : 10008}<br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;51054d998217798d9fc43548&#8221;} , &#8220;empId&#8221; : 10009}<br />
Done</div>
<p>
</div>
<p><b>Reference-</b></p>
<p><a href="http://www.mongodb.org/display/DOCS/Advanced+Queries" target="_blank">MongoDB Advance Queries</a></div>
<p><b>Download Source Code + Libs</b><br />
<a href="https://sites.google.com/site/dinesh9582486434/my-forms/MongoDBDeleteDemo.zip?attredirects=0&;d=1" target="_blank"><b>MongoDBDeleteDemo.zip </b></a></p>
<div style="background-color: #ff99cc;"> ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;<b> ; ; ; ; <;<;<a href="https://dineshonjava.com/2013/01/java-mongodb-inserting-document-to.html">previous</a><;<; ; ; ; ; ; ; ; ; ; ; ; ; || <a href="https://dineshonjava.com/2013/01/introduction-to-mongodb.html">index ; </a>||  ;  ;  ;  ; >;>;<a href="https://dineshonjava.com/2013/01/java-mongodb-updating-document-to.html">next</a>>;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/java-mongodb-inserting-document-to/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/java-mongodb-updating-document-to/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '590'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…