Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb shell scripting

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();
} 
like image 645
breedish Avatar asked Jan 18 '11 17:01

breedish


People also ask

Is MongoDB a scripting language?

It is a simple language to learn and feels similar to programming, however it is not programming.

What language is used in MongoDB shell?

MongoDB uses the MongoDB Query Language (MQL), designed for easy use by developers. The documentation compares MQL and SQL syntax for common database operations.


2 Answers

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() );
}
like image 70
pingw33n Avatar answered Oct 05 '22 00:10

pingw33n


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

db = db.getSiblingDB("otherdb") //same as use otherdb
like image 43
csgwro Avatar answered Oct 05 '22 01:10

csgwro