即使我已连接并使用该数据库,Mongo shell也会抛出“ReferenceError db未定义”?

Nic*_*eat 1 mongodb referenceerror mongo-shell


nicole@MacBook-Air client % mongosh
Current Mongosh Log ID: 6167b26073f2085eee4554b6
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:          5.0.3
Using Mongosh:          1.1.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting:
   2021-10-13T15:36:00.307-07:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test> show dbs
admin     41 kB
config   111 kB
local     41 kB
test    8.19 kB
test> use test
already on db test
test> show collections
users
test> test.users.find()
ReferenceError: test is not defined
test> users.find()
ReferenceError: users is not defined
test> 
Run Code Online (Sandbox Code Playgroud)

我是 mongo 新手,刚刚在我的 Mac(Big Sur 11.4)上安装了本地数据库。

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

brew tap mongodb/brew
brew install mongodb-community@5.0
brew services start mongodb-community@5.0
Run Code Online (Sandbox Code Playgroud)

我刚刚users使用猫鼬通过快速服务器制作了一个集合。然后我在终端中打开数据库来检查内容。但是,我收到一个非常奇怪的错误,告诉我数据库test未定义,即使 shell 告诉我正在使用该test数据库。还说集合users未定义,但它在那里?

我究竟做错了什么?

Wer*_*eit 7

你的命令是错误的。使用其中之一:

use test
db.users.find()
db.getCollection("users").find()

db.getSiblingDB("test").users.find()
db.getSiblingDB("test").getCollection("users").find()
Run Code Online (Sandbox Code Playgroud)

  • 我认为 db 是数据库名称的占位符。谢谢,现在可以了。 (7认同)