How to delete all documents in mongodb collection in java

前端 未结 4 912
半阙折子戏
半阙折子戏 2020-12-17 09:59

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         


        
4条回答
  •  孤城傲影
    2020-12-17 10:36

    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();
    

提交回复
热议问题