the_yaz2000
the_yaz2000

Reputation: 141

Why the result of db.user.count() is 0 in mongo?

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

Upvotes: 0

Views: 167

Answers (1)

R2D2
R2D2

Reputation: 10737

  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.

Upvotes: 1

Related Questions