MongoDB Bulk Find and Remove – How to Reduce Time

bulk-insertmongodb

I'm just making my hands dirty(handson) with MongoDB. First I did bulk insert of 1 million documents.

// Bulk insert query
var bulk = db.bulktest.initializeUnorderedBulkOp();
for(var i=0;i<1000000;i++){bulk.insert({a:i});}
bulk.execute();

It took 10 seconds to insert all the document which is comparatively good. None of the documents were missed.

However, when it comes to bulk find and removal, the performance has drastically came down and is really bad as it is removing each document in the order of 10ms.

// find and removal bulk query
var bulk = db.bulktest.initializeUnorderedBulkOp();
for(var i=0;i<1000000;i++){bulk.find({a:i}).remove();}
bulk.execute();

Has anyone done tweaking around bulk removal ?

I've only one option to drop the complete collection and I don't want to do this.

Best Answer

This shouldn't have anything to do with bulk operations. The find operation must read through the collection, which takes time on 1million documents.

Adding an index on a should improve the speed dramatically.