When running dataSize in a mongos, the command only works when the current database matches the database of the collection whose size you're trying to measure.
For example, if I have a collection test.users, this works:
mongos> use test switched to db test mongos> db.runCommand({"dataSize": "test.users", "keyPattern": {"username": 1}, "min": {"username": 0}, "max": {"username": 7000}}) { "size" : 200000, "numObjects" : 5000, "millis" : 5, "ok" : 1 }
However, if the current database is not test, then the shell erroneously reports that the collection is empty:
mongos> use admin switched to db admin mongos> db.runCommand({"dataSize": "test.users", "keyPattern": {"username": 1}, "min": {"username": 0}, "max": {"username": 7000}}) { "size" : 0, "numObjects" : 0, "millis" : 0, "ok" : 1 }
Since the full namespace is required for the command, it makes no sense to consider the currently active database.
This problem only occurs when connected to a mongos. If I connect directly to a mongod, dataSize correctly gives me the collection size no matter what the currently active database is:
> use admin switched to db admin > db.runCommand({"dataSize": "test.users", "keyPattern": {"username": 1}, "min": {"username": 0}, "max": {"username": 7000}}) { "estimate" : false, "size" : 960, "numObjects" : 24, "millis" : 0, "ok" : 1 }