【问题标题】:Can't open collection in the MongoDB shell无法在 MongoDB shell 中打开集合
【发布时间】:2025-12-14 07:05:01
【问题描述】:

我无法在 MongoDB 命令提示符中打开集合。我可以添加用户、更新用户、通过 Postman 等获取用户...

第一次发生这种情况是几个月前删除用户。首先,db.database.find() 没有什么可显示的。但是在重新启动我的本地 Windows10 机器后,它又可以工作了。但是现在重新启动甚至不起作用。我想打开集合the proper way。有什么想法吗?

MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.4.6
> show dbs
admin    0.000GB
user  0.000GB
local    0.000GB
> use user
switched to db user
> show collections

profiles
users
> use users
switched to db users
> db.users.find()

// then nothing happens anymore

【问题讨论】:

  • 执行完全相同的命令序列,但没有最后一个 use users 命令。 use 选择一个数据库,而不是一个集合。 db.users.find() 的细分是 db 指的是所选数据库(在本例中为 user),users 是所选数据库中的集合,find 是查询。

标签: node.js mongodb


【解决方案1】:

你正在切换到 db users 你没有名为 users 的 db 只是不要使用 use users 而只是使用 db.users.find()

MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.4.6
> show dbs
admin    0.000GB
user  0.000GB
local    0.000GB
> show collections

profiles
users


> db.users.find()

use 命令用于数据库,因此您不应该使用它来检出集合。

【讨论】: