首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongo shell抛出"ReferenceError db is not defined“,即使我已经连接并使用那个db?

Mongo shell抛出"ReferenceError db is not defined“,即使我已经连接并使用那个db?
EN

Stack Overflow用户
提问于 2021-10-14 04:39:13
回答 1查看 496关注 0票数 0
代码语言:javascript
复制
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/

代码语言:javascript
复制
brew tap mongodb/brew
brew install mongodb-community@5.0
brew services start mongodb-community@5.0

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

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-14 07:40:37

你的命令是错的。使用以下选项之一:

代码语言:javascript
复制
use test
db.users.find()
db.getCollection("users").find()

db.getSiblingDB("test").users.find()
db.getSiblingDB("test").getCollection("users").find()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69565158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档