默认情况下,在mongodb中连接到特定数据库

编程入门 行业动态 更新时间:2024-10-27 07:27:52
本文介绍了默认情况下,在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中连接到特定数据库

本文发布于:2023-10-17 11:10:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   情况下   数据库   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!