sudesh
sudesh

Reputation: 973

Selecting database inside the js in mongoDB

I can run the script in the specific database like

 mongo main myscript.js<br/>

where main is a database name. The same thing I am unable to run inside the script like

myscript.js

 use main;   // not taking, throw error when using "mongo myscript.js"

Upvotes: 4

Views: 3810

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230356

Use this in your script

db = db.getSiblingDB('main');

The default DB is test. This statement gets a "sibling" database of test.

Upvotes: 11

Related Questions