默认情况下,在mongodb中连接到特定数据库 [英] Connect to a specific database by default in mongodb

查看:1344
本文介绍了默认情况下,在mongodb中连接到特定数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Linux机器上运行mongodb.因此,每次我从控制台连接到它(键入mongo)时,都会得到类似这样的信息:

I am running a mongodb on a linux box. So every time I connect to it from the console (typing mongo) I get something like this:

MongoDB shell version: 2.4.9
connecting to: test

然后我正在使用myDatabase(其中myDatabase为99%相同).所以基本上我总是做一些不必要的工作.有没有一种方法可以配置mongo,以便默认情况下它将连接到myDatabase?

And then I am doing use myDatabase (where myDatabase is 99% is the same). So basically I always do some unneeded type of work. Is there a way to configure mongo, so that it will connect to myDatabase by default?

推荐答案

令人惊讶的是,我没有找到与此类似的副本.好的,现在我们有内容了.

Surprised that I don't find a duplicate of this. Okay, now we have content.

在命令行中,只需执行以下操作:

From the command line, just do this:

$ mongo myDatabase

$ mongo myDatabase

文档中实际上涵盖了此内容.向下翻页.没有直接链接,但搜索<db address>,并且存在相同的示例.

This actually is covered in the documentation, albeit down the page somewhat. No direct link but search for <db address> and the same example is there.

您当然可以做到:

$ mongo --help
MongoDB shell version: 2.4.9
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
    foo                   foo database on local machine
    192.169.0.5/foo       foo database on 192.168.0.5 machine
    192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999

其中显示了用法以及您可以传递的其他选项.

Which shows the usage along with other options you can pass in.

另一件事,不是默认的连接,而是一种快捷方式,您可以在.mongorc.js文件中执行此操作:

Another thing, not quite a default connect but a shortcut is you can do this in the .mongorc.js file:

db=db.getSiblingDB("myDatabase")

现在将哪个变量db分配给该数据库,所以:

Which assigns the variable db to that database so now:

db.collection.find()

正在对myDatabase起作用.

这篇关于默认情况下,在mongodb中连接到特定数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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