Remove() Vs Drop() in MongoDB

Once we have documents stored in our collection , we can remove all of the documents from it in two ways. Now choosing one over other is totally depends on your requirement.

  • Using remove()
  • Using drop()

This post will show a comparison on Remove() Vs Drop() in MongoDB.

remove() vs drop() in mongodb

Before using one of them lets try to look into the pros and cons of each other.

  1. Using Remove():  remove has two overloaded versions ,one in which we will pass the criteria to remove all the documents that are matching our passed criteria and 2nd one is default where we won’t pass any criteria (prior to 2.6) or pass an empty document (version 2.6 or more) and it will remove all the documents from the collection. Here, we are more interested in 2nd version when our intention is to clear all the documents from a collection. Let’s have a look at the code for removing all the documents. Here , I have inserted 100,0000 (1 million) documents in my collection and after that I am using remove() to delete all the documents.
    public static void insertAndRemove(){
    		DBCollection collection = MongoRemoveAndDrop.getCollection();
    		//Inserting 1000,000 documents into collection
    		for(int i=0;i<1000000;i++){
    			BasicDBObject object = new BasicDBObject();
    			object.put("identification", i);
    			object.put("role", "Role "+i);
    			collection.insert(object);
    		}
    		//Remove all the documents
    		BasicDBObject removeObject = new BasicDBObject();
    		long currentTime = System.currentTimeMillis();
    		collection.remove(removeObject);
    		System.out.println("Removal Time : "+(System.currentTimeMillis()-currentTime));
    	}

    When I invoked this method the removal time comes around approximately 9906 ms. Internallyy remove() deletes all the documents in the collection , delete the index entry pointing to the original document and lot of other housekeeping activities. Kindly note only index entries will be deleted not the actual index.
    Below are the pros and cons of using remove()

    • Pros :
      1.  Structure of collection remains intact..
      2.  Index won’t be dropped.
    • Cons :
      1.  Less efficient take more time in comparison to its counterpart drop()
  1. Using Drop(): By invoking drop() on a collection , it will remove all the documents from it ,it will delete all the indexes on it  and at the end it will delete the collection itself. Let’s have a look at the code for removing all the documents using drop(). Here , I have inserted 100,0000 (1 million) documents in my collection and after that I am using drop() to delete all the documents.
    public static void insertAndDrop(){
    		DBCollection collection = MongoRemoveAndDrop.getCollection();
    		//Inserting 1000,000 documents into collection
    		for(int i=0;i<1000000;i++){
    			BasicDBObject object = new BasicDBObject();
    			object.put("identification", i);
    			object.put("role", "Role "+i);
    			collection.insert(object);
    		}
    		//Drop the collection
    		long currentTime = System.currentTimeMillis();
    		collection.drop();
    		System.out.println("Drop Time : "+(System.currentTimeMillis()-currentTime));	
    	}

    When I invoked this method the total time it took for dropping the collection is 60  ms , which is much less than what we got from remove(). But drop has also its pros and cons.

    • Pros :
      1. More efficient as time for deleting all the documents is very less.
    • Cons :
        1.  Entire collection will be dropped.
      2.  Indexes will also be removed.

By default every collection has indexes on _id ( more about _id can be read here) and if the collection from which you are removing the documents has index on _id  only, then you can safely use drop(). If your collection has indexes on other key then kindly note that you have to recreate recreate index after using drop().
remove() vs drop() in mongodb

package com.lotusmedia.mongo;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

public class MongoRemoveAndDrop {
	private static final int MAX_DOCS=1000000;

	private static DBCollection getCollection() {
		MongoClient serverConnection = null;
		try {
			serverConnection = new MongoClient("localhost",27017);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw new RuntimeException("Not able to connect to localhost");
		}
		DB db = serverConnection.getDB("lotus");
		return db.getCollection("lotusCollection");
	}

	public static void insertAndRemove(){
		DBCollection collection = MongoRemoveAndDrop.getCollection();
		//Inserting 1000,000 documents into collection
		for(int i=0;i<MAX_DOCS;i++){
			BasicDBObject object = new BasicDBObject();
			object.put("identification", i);
			object.put("role", "Role "+i);
			collection.insert(object);
		}
		//Remove all the documents 94257 48481
		BasicDBObject removeObject = new BasicDBObject();
		long currentTime = System.currentTimeMillis();
		collection.remove(removeObject);
		System.out.println("Removal Time : "+(System.currentTimeMillis()-currentTime));
	}

	public static void insertAndDrop(){
		DBCollection collection = MongoRemoveAndDrop.getCollection();
		//Inserting 1000,000 documents into collection
		for(int i=0;i<MAX_DOCS;i++){
			BasicDBObject object = new BasicDBObject();
			object.put("identification", i);
			object.put("role", "Role "+i);
			collection.insert(object);
		}
		//Drop the collection
		long currentTime = System.currentTimeMillis();
		collection.drop();
		System.out.println("Drop Time : "+(System.currentTimeMillis()-currentTime));	
	}

	public static void main(String[] args) {
		MongoRemoveAndDrop.insertAndRemove();
		MongoRemoveAndDrop.insertAndDrop();
	}

}

Disclaimer : All the above used images belong to their respective owners.


 


Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect
Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>