Search code examples
databasemongodbshardingmongo-shell

Mongos shell not displaying the collections that are created on shards


I have a 3 node sharded cluster with mongos installed on a seperate node. When database (test) is created using mongos shell and the collections are added,I am able to view the collections using

use test

show collections

However when the database (test2) is created in one of the shard node (not using mongos).

mongo --host=hostname --port=port

use testdb2

db.collection1.insert({"testing_user":"test"})

and I am able to view the collection from this shard

However when I login to mongos shell, it shows the collections of testdb2 to be empty.

Also, I tried to check if the collection is present in the config database.

use config

db.collection.find()

I don't see the collections that were added to testdb2


Solution

  • However when the database (test2) is created in one of the shard node (not using mongos).

    Don't do that.

    The whole point of a sharded cluster is to divide the data between shards. The config servers assign and maintain records of where each database/collection is stored, and the mongos routers transparently direct queries to the appropriate shard(s).

    If you create a collection by connecting directly to a shard, the mongos and config servers are not aware of it, so the mongos cannot be expected to know about it. In most cases that data will be inaccessible, and in some cases it may be deleted.

    Bottom line is if you are using a sharded mongodb cluster, the only safe way to insert and access data is via the mongos.