Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting database inside the js in mongoDB

Tags:

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"
like image 204
sudesh Avatar asked Dec 27 '22 07:12

sudesh


1 Answers

Use this in your script

db = db.getSiblingDB('main');

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

like image 119
Sergio Tulentsev Avatar answered Jan 18 '23 15:01

Sergio Tulentsev