为什么db.user.count()的结果在mongo中为0?

发布于 2025-02-06 17:57:47 字数 464 浏览 0 评论 0原文

正如您在下图中看到的那样0. “在此处输入图像描述“

)的图像

db.collection.count ( “ https://i.sstatic.net/9tunh.jpg” alt =“在此处输入图像说明”>

我在此集合上确实有一个文档

as you can see in the image below I just create an user even though it still gave 0 , and the same thing with collections when I run show collection it shows me one , but when when I do db.collection.count() gave me 0.enter image description here

the image for db.collection.count()

enter image description here

I do have one document on this collection here is image from Mongo compass
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯寂 2025-02-13 17:57:47
  1. 如您所见,此数据库中没有“用户”集合,只有一个集合,其名称是“ databases_for_tp”,因此在不现有集合中的计数文档将始终显示为0
  2. 。计算单个集合中有多少个文档,而不是所选数据库中有多少个集合。
  3. 当您在管理数据库中创建用户时,即使创建它们是在不同数据库中进行身份验证的,您也可以找到它们。

检查:

 use admin
 show collections
 db.system.users.count()

请注意,将代码添加为文本而不是图片是一个好习惯,以便更容易解释。

  1. As you can see there is no "user" collection in this database , there is only one collection and its name is "Databases_for_tp" so counting documents in not existing collection will show always 0.
  2. You misenterpreted the meaniing of count() command , it counts how many documents there is in single collection , not how many collections there is in chosen database.
  3. You can find the users when you create them in the admin database even they are created to authenticate in different database.

check:

 use admin
 show collections
 db.system.users.count()

Note also it is a good practice to add code as text and not as pictures so it is easier to interpret.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文