为MongoDB Shell设置默认数据库 [英] Setting default database for MongoDB shell

查看:196
本文介绍了为MongoDB Shell设置默认数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我进入终端的mongo shell时,它总是从数据库测试开始,这是错误的数据库.您可以将mongo设置为在特定数据库中启动吗?

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?

推荐答案

命令行

您可以选择要在 mongo命令行,例如'mydb':

Command Line

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

mongo mydb

如果未提供数据库名称,则将使用'test'.

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

如果您想设置默认数据库而不每次都在命令行上指定,则可以在

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")

.mongorc.js文件在启动mongo shell之后执行,因此,如果在此处设置默认值,它将覆盖命令行上指定的数据库.

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.

这篇关于为MongoDB Shell设置默认数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆