我是 mongo 的新手,目前正在尝试使用 mongo shell 设置我项目的数据库。我从概念上理解集合就像一张表,而数据库就是集合。
我对以下内容感到困惑:
- 'db' 是我正在使用的数据库吗?我读到初始数据库是“test”和“db”。是哪一个?
- 当我使用像
db.bears.find()
这样的命令时,bears 是数据库还是集合?当我同时使用命令show dbs
和db.getCollectionNames()
时,两个命令中都列出了熊!它们如何成为集合和数据库? - 嵌套数据库是否可能?
最佳答案
is 'db' my database I'm working in? I read that the initial database is 'test' and 'db'. Which one is it?
db
指的是你当前使用的数据库。默认情况下,当您第一次打开 mongo 时,该数据库称为 test
。
您可以像这样切换到不同的数据库(例如称为 test2):
>use test2
switched to db test2
现在 db
将引用 test2
,您正在使用的新数据库。
When I use a command like 'db.bears.find()', Is bears a database or collection? When I use both commands 'show dbs' and 'db.getCollectionNames()', 'bears is listed in both! How can they be collections and databases?
当您在该命令中编写 db.bears.find()
时,bears 是一个集合。如果熊同时出现在 show dbs
和 db.getCollectionNames()
中,这意味着您有一个名为 bears 的集合和一个名为 bears 的数据库。它们是两个独立的实体。
Is nested databases possible?
如果我正确理解你的问题,不,这是不可能的。
关于mongodb - 对数据库与集合感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30732890/