I want to delete all documents in a collection in java. Here is my code:
MongoClient client = new MongoClient("10.0.2.113" , 27017);
MongoDatabase db = client.getDatabase("maindb");
db.getCollection("mainCollection").deleteMany(new Document());
Is this the correct way to do this?
I am using MongoDB 3.0.2
For newer mongodb drivers you can use FindIterable to remove all documents in collection.
Using API >= 3.0:
To drop the collection (documents and indexes) you still can use:
see https://docs.mongodb.org/getting-started/java/remove/#remove-all-documents
To remove all documents use the BasicDBObject or DBCursor as follows:
If you want to remove all documents in collection then used below code :
Or If you want to drop whole collection then used this :