8

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

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 am using 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 an output I have

!ERROR! ns not found

shiny_db.physicalinfos

Dunno what to do right now. I'll be very thankful for your help.

8
  • Don't delete anything that starts with system as those are reserved for use by MongoDB. Commented Jul 8, 2013 at 20:09
  • Can you tell me hove to avoid them? Commented Jul 10, 2013 at 11:18
  • just check to see if the string starts with system. Commented Jul 10, 2013 at 12:15
  • like this? if (name.substring(0, 6) == "system") Commented Jul 10, 2013 at 12:46
  • Nearly, it's substring(from, to), so (0, 5) and using === would be considered best practice for JavaScript. Commented Jul 10, 2013 at 13:30

3 Answers 3

8

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

db.dropDatabase();

At least from the Mongo CLI, whenever you access an inexistent DB, it'll be persisted the soon you create data. That's the same as dropping all collections from it.

I haven't tried MongoDB for anything except studying yet, so I don't know much about permissions. So, probably the only problem of dropping the entire DB would be the permissions of your users that would be lost (I believe).

If this script you're trying to create is not for production, then you're good to go with dropping the DB.

Sign up to request clarification or add additional context in comments.

1 Comment

well, it wasn't my goal ;)
3

I found an answer. First of all I've made mistake in my connection it should be like following: 'mongodb://127.0.0.1:27017/shiny_db'. The second mistake was in the name of collection. It was like 'db_name.coll_name', that's why db.dropCollection(name, callback) couldn't find particular collection and because of it I had mistake ns not found. So I've used following mechanism to separate db_name from coll_name:

var name = colls[i].name.substring('shiny_db.'.length); and I've added checking for "system" collection.

Final code looks like following:

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's a system file");
        }    
    } 
}

Hope it will help someone!

Comments

0

listCollections gives you an array of collection names as strings.

It looks like you may be confusing it with something that returns an array of collection objects like maybe db.collections()

3 Comments

But what the difference what to use for defining a collection name? I just need names of collections to use them as arguments in db.dropCollection(name, callback)
if it gives you back an object you need to use obj.name to get the name. if it gives you back names, you shouldn't use name.name.
Well, I've checked what collectionNames returns, and it returns a collection with object, that have a field "name". But your answer bring me one idea that was successful, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.