How to delete all documents in mongodb collection in java
Asked Answered
C

4

25

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

Clarkson answered 25/6, 2015 at 18:49 Comment(2)
you want to remove specific matched documents or drop whole collection?Armenta
All the documents in the collection.Clarkson
F
21

To remove all documents use the BasicDBObject or DBCursor as follows:

MongoClient client = new MongoClient("10.0.2.113" , 27017);
MongoDatabase db = client.getDatabase("maindb");
MongoCollection collection = db.getCollection("mainCollection")

BasicDBObject document = new BasicDBObject();

// Delete All documents from collection Using blank BasicDBObject
collection.deleteMany(document);

// Delete All documents from collection using DBCursor
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
    collection.remove(cursor.next());
}
Floe answered 25/6, 2015 at 18:57 Comment(1)
Whats the difference between these 2 approaches ?Faggoting
W
29

Using API >= 3.0:

MongoClient mongoClient = new MongoClient("127.0.0.1" , 27017);
MongoDatabase db = mongoClient.getDatabase("maindb");
db.getCollection("mainCollection").deleteMany(new Document());

To drop the collection (documents and indexes) you still can use:

db.getCollection("mainCollection").drop();

see https://docs.mongodb.org/getting-started/java/remove/#remove-all-documents

Warrin answered 26/1, 2016 at 13:0 Comment(0)
F
21

To remove all documents use the BasicDBObject or DBCursor as follows:

MongoClient client = new MongoClient("10.0.2.113" , 27017);
MongoDatabase db = client.getDatabase("maindb");
MongoCollection collection = db.getCollection("mainCollection")

BasicDBObject document = new BasicDBObject();

// Delete All documents from collection Using blank BasicDBObject
collection.deleteMany(document);

// Delete All documents from collection using DBCursor
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
    collection.remove(cursor.next());
}
Floe answered 25/6, 2015 at 18:57 Comment(1)
Whats the difference between these 2 approaches ?Faggoting
A
10

If you want to remove all documents in collection then used below code :

 db.getCollection("mainCollection").remove(new BasicDBObject());

Or If you want to drop whole collection then used this :

db.getCollection("mainCollection").drop();
Armenta answered 25/6, 2015 at 18:55 Comment(1)
Advise against using drop() to truncate a collection if you are going to keep using it. You may get an erratic error 'Operation aborted because: all indexes on collection dropped'. This is apparently because index destroy is asynchronous.Flap
E
0

For newer mongodb drivers you can use FindIterable to remove all documents in collection.

FindIterable<Document> findIterable = collection.find();
       for (Document document : findIterable) {
         collection.deleteMany(document);
       }
Edvard answered 26/7, 2019 at 2:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.