How to delete all collections via mongodb and node.js?

I am new to node.js and mongodb and I have the following problem: I need to delete all collections from my mongodb from the file node.js. I have a function like this:

service.dropCollections = function(db, colls){ for(var i = 0; i < colls.length; i++){ var name = colls[i].name; db.dropCollection(name, function(err) { if(!err) { console.log( name + " dropped"); } else { console.log("!ERROR! " + err.errmsg); } }); } } 

And I use it in the following function:

 service.clearDB = function() { var MongoClient = require('mongodb').MongoClient , format = require('util').format; MongoClient.connect('mongodb://127.0.0.1:27017/shiny_d', function(err, db){ if(err) throw err; db.collectionNames(function(err, collections){ if(!err){ service.dropCollections(db, collections); } else { console.log("!ERROR! "+ err.errmsg); } service.showCollections(); }); }); } 

As a conclusion, I have

! MISTAKE! ns not found

shiny_db.physicalinfos

I don’t know what to do right now. I will be very grateful for your help.

+8
mongodb
source share
3 answers

I have found the answer. First of all, I made a mistake in my connection, it should look like this: 'mongodb://127.0.0.1:27017/shiny_db' . The second mistake was in the name of the collection. It was like 'db_name.coll_name' , so db.dropCollection(name, callback) could not find a specific collection, and because of this I was wrong ns not found . Therefore, I used the following mechanism to separate db_name from coll_name:

var name = colls[i].name.substring('shiny_db.'.length); , and I added a check for the "system" collection.

The resulting code is as follows:

 service.clearDB = function() { var MongoClient = require('mongodb').MongoClient , format = require('util').format; MongoClient.connect('mongodb://localhost/shiny_db', function(err, db) { if(err) throw err; db.collectionNames(function(err, collections){ if(!err){ service.dropCollections(db, collections); } else { console.log("!ERROR! "+ err.errmsg); } }); }); } service.dropCollections = function(db, colls){ for(var i = 0; i < colls.length; i++){ var name = colls[i].name.substring('shiny_db.'.length); if (name.substring(0, 6) !== "system") { db.dropCollection(name, function(err) { if(!err) { console.log( name + " dropped"); } else { console.log("!ERROR! " + err.errmsg); } }); } else { console.log(name + " cannot be dropped because it a system file"); } } } 

Hope this helps someone!

+3
source share

Isn't it faster, easier, and less error prone if you just drop the entire database?

 db.dropDatabase(); 

At least from the Mongo CLI, whenever you access a non-existent database, it will be saved as soon as you create the data. Same as removing all collections from it.

I have not tried MongoDB for anything other than learning, so I know little about permissions. Thus, probably the only problem with deleting the entire database is the permissions of your users that will be lost (I suppose).

If this script that you are trying to create is not intended for production, then you can refuse to delete the database.

+7
source share

listCollections gives you an array of collection names as strings.

It looks like you can confuse it with something that returns an array of collection objects, like db.collections()

0
source share

All Articles