Home > Back-end >  Is there an easy way to remove all data from MongoDB collection in C#?
Is there an easy way to remove all data from MongoDB collection in C#?

Time:08-30

I want to make a unique method for deleting all data from MongoDB collection in C#.

Before I called only RemoveAll() method and it worked. But now as it has only DeleteOne() i DeleteMany() methods where I should put filter, but as this method should be unique for all collections, I am not sure how to make filter to remove all data from collection without deleting collection.

For Collection it is not specified which one, it should go as a parameter to the method.

CodePudding user response:

Try using an empty BsonDocument to delete all the documents from the collection:

collection.DeleteMany(new BsonDocument());
  • Related