MongoDb Shell Scripts

Do you know if you can get a list of databases (for example, "show dbs" in the console) from javascript. I want to delete all databases from mongo through a javascript file (mongo admin.js), but I cannot find a way to list all the databases ...

thanks

I am trying to prepare a simple script, but I cannot find out how I can change the db from the script. Here is an example javascript script. It always fails in the "use" command. 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(); } 
+8
javascript mongodb nosql
source share
2 answers

Use db.adminCommand('listDatabases') . For other teams 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() ); } 
+12
source share

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

 db = db.getSiblingDB("otherdb") //same as use otherdb 
+7
source share

Source: https://habr.com/ru/post/651221/


All Articles