Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete lots of mongodb collections at once?

Tags:

There are a lot of mongodb collections in my database that I need to delete. They all have similar names, and it would be easy to delete them if only wildcard characters could be used. But it doesn't look like they can.

Is there a way to select a bunch of collections at once to delete them?

like image 552
node ninja Avatar asked Jun 26 '12 12:06

node ninja


People also ask

How do I delete multiple collections in MongoDB?

In MongoDB, you are allowed to delete the existing documents from the collection using db. collection. deleteMany() method. This method deletes multiple documents from the collection according to the filter.

What is the fastest operation to clear an entire collection in MongoDB?

MongoDB's db. collection. drop() is used to drop a collection from the database.

How do I empty a MongoDB collection?

To delete a MongoDB Collection, use db. collection. drop() command.

How do I delete multiple records in MongoDB Atlas?

You actually can't bulk delete in MongoDB Atlas. See MongoDB Atlas info on the filter section AKA Data Explorer. However you can run standard queries, like find, remove once you connect to the database using your Atlas credentials.


1 Answers

For regex you can use string.match

db.getCollectionNames().forEach(function(c) {     if(!c.match("^system.indexes")) {          db.getCollection(c).drop();     }   }); 
like image 149
Yogesh Mangaj Avatar answered Oct 06 '22 01:10

Yogesh Mangaj