Mongo shell 抛出 "ReferenceError db is not defined" 即使我已连接并使用该数据库?

Mongo shell throws "ReferenceError db is not defined" even when I am connected and using that db?


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> 

我是 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

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

我做错了什么?

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

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

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