0

As explained here, db.bios.remove() deletes all the documents from the bios collection of currently connected DB. What is the equivalent method for this functionality in MongoDB Java API?

I did not find any methods like DB.removeCollection(..) or DBCollection.removeAll(). Please help me.

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • I cannot add a link only answer so here: http://api.mongodb.org/java/current/com/mongodb/DBCollection.html#remove(com.mongodb.DBObject) – Neil Lunn Mar 18 '14 at 12:35
  • have you looked here http://api.mongodb.org/java/2.5/com/mongodb/DBCollection.html#remove(com.mongodb.DBObject, com.mongodb.WriteConcern) ? – Flot2011 Mar 18 '14 at 12:36
  • I am aware of `DBCollection.remove(DBObject)` method, but I dont know how to use it to remove all – sanbhat Mar 18 '14 at 12:39

1 Answers1

1

You can do that by simply calling the remove method with an empty object.

yourDBCollection.remove(new BasicDBObject());

The equivalent Mongo shell command would be db.bios.remove({});

Remember that MongoDB query objects work like filters: They let anything through which doesn't explicitely violates them.

Philipp
  • 67,764
  • 9
  • 118
  • 153