Is there a way to drop all the collections in a MongoDB database using one command, without needing to name each individual collection?
From what I've found, it looks like the command to do this is db.dropDatabase()
. But that gives me an Unauthorized error:
> use MyDb
switched to db MyDb
> db.dropDatabase();
{
"ok" : 0,
"errmsg" :
"not authorized on MyDb to execute command
{ dropDatabase: 1.0, $db: \"MyDb\" }",
"code" : 13,
"codeName" : "Unauthorized"
}
However, I can drop each collection in the db with no problem using db.collection.drop()
.
> use MyDb
switched to db MyDb
> db.collection1.drop();
true
> db.collection2.drop();
true
> db.collection3.drop();
true
So is there a way to drop all collections in a MongoDB database other than using db.dropDatabase()
?