Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

Setting default database for MongoDB shell

When I go into the mongo shell in my terminal, it always starts with the database test, which is the wrong database. Can you set mongo to start in a specific database?

question from:https://stackoverflow.com/questions/11336346/setting-default-database-for-mongodb-shell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Command Line

You can select the database to use on the mongo command line, eg for 'mydb':

mongo mydb

If a database name is not provided, 'test' will be used.

In .mongorc.js

If you want to set a default database without specifying on the command line each time, you can add a line to the .mongorc.js file in your home directory:

db = db.getSiblingDB("mydb")

The .mongorc.js file is executed after the mongo shell is started, so if you set a default here it will override a database specified on the command line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...