1

Mongo server: Windows 10 (host) client: CentOS 6.2, a virtual box vm on windows 10 host. This is actually a cloudera quick start vm. Issue: mongodb connects to the remote server (from CentOS to Windows) via terminal, lists the databases fine, but 'show collections' just returns blank. That said, the collections are accessible because I can query any collection and the count also gives me the correct results. On the other hand, I have connected to the same mongo server from IntelliJ and it shows all the collections just fine.

Just curious as to why this is happening.. Any comments?

Side Note: is there a mongodb command to count the number of collections in a database?

Thanks

_Vamsi

Vamsi
  • 52
  • 10

1 Answers1

0

Make sure you are using the database you want to show the collections for. You may be using a database that doesn't have any collections.

> use desiredDatabase
> show collections

If the list is still empty, try signing in with an admin user account. The user needs to be able to perform the listCollections action. The dbAdmin role includes the listCollections action.

To get the count you can use the getCollectionNames function which returns an array and you can get the length from that.

> db.getCollectionNames().length
logan rakai
  • 2,519
  • 2
  • 15
  • 24
  • Thanks Logan. Of course I'm using the right database. The second piece of information is good. Tahnks for that. – Vamsi Dec 08 '16 at 20:27
  • If you create a collection from the terminal does it show up? E.g. `db.collection.insert({a:1})` then `db.getCollectionNames()` shows `collection`? – logan rakai Dec 08 '16 at 20:35
  • I can add a collection, no issues. But 'show collections' and db.getCollectionNames() both show blank. However, i can query the new collection just fine though. Weird. – Vamsi Dec 08 '16 at 21:18
  • Must be because the user you sign in with doesn't have appropriate permission. I'll update the answer. – logan rakai Dec 08 '16 at 22:03
  • Logan: Now I'm able to access the collections. My hunch is at that time, may be i was using the default `test` collection and did not actually switch to the needed collection. Thanks for your response. – Vamsi Jan 06 '17 at 19:35