8

Do you know if it is possible to get a list of databases(like 'show dbs' in console) from javascript. I want to drop all databases from mongo via javascript file (mongo admin.js), but i can't find a way to list all databases...

Thx

I'm trying to prepare simple script but i can't find out how i can change db from script. Here is the sample javascript script. It always fails on command "use". I tried with db.eval and eval but it fails.

print(db.getMongo().getDBNames());
var environments = db.getMongo().getDBNames()
for(var environmentIndex in environments){
    print(environments[environmentIndex])   
    eval("use staging");
    //db.dropDatabase();
} 
1

2 Answers 2

13

Use db.adminCommand('listDatabases'). For other commands see http://www.mongodb.org/display/DOCS/List+of+Database+Commands

EDIT:

In util.js use dbname is defined as:

shellHelper.use = function( dbname ){
    db = db.getMongo().getDB( dbname );
    print( "switched to db " + db.getName() );
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes, thx. But how i can switch to different db via script? I updated description with sample script.
@pingw33n Can we run the mongodb commands as scripts in bash/linux without having mongo shell installed on machine?
7

http://www.mongodb.org/display/DOCS/Scripting+the+shell

db = db.getSiblingDB("otherdb") //same as use otherdb

Comments

Your Answer

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