As a result of SERVER-44891 which was closed as Won't Fix I created this workaround. This script can be verified as follows using mtools, m and mgeneratejs:
First, initialize the cluster:
mkdir shardedCluster
m 4.0.9
mlaunch init --replicaset --nodes 1 --sharded 60 --csrs --binarypath $(m bin 4.0.9) --dir shardedCluster --auth
Next, log into the cluster (mongo --norc -u user -p password --authenticationDatabase admin
) and run the following:
sh.enableSharding("test")
sh.shardCollection("test.test1", { statusID: 1 } )
db.getSiblingDB("config").settings.save( { _id:"chunksize", value: 1 } )
Exit the mongo shell, and from bash using mgeneratejs
seed the cluster:
mgeneratejs -n 500000 '{statusID: {"$numberLong": {"min": 100, "max": 100000}},metadata: {blacklistType: {"$numberLong": {"min": 1020, "max": 1100}}},groupID: {"$numberLong": {"min": 1600, "max": 5000}},departmentID: {"$numberLong": {"min": 1600, "max": 5000}},assignedToID: {"$choose": {"from": ["", "123", "456"], "weights": [1, 2, 2]}},allowedUsers: {"$choose": {"from": ["", "345", "678"], "weights": [1, 2, 2]}},typeID: {"$numberLong": {"min": 100750, "max": 100760}},isFileProhipted: "$bool",fileWriter: {"$choose": {"from": ["", "xxx", "yyy"], "weights": [1, 2, 2]}},assignedByID: {"$numberLong": {"min": 200750, "max": 200760}}}' | mongoimport -u user -p password -c test1
Once seeded, connect to the cluster again and create a large number of indexes:
db.test1.createIndexes([
{ "statusID":1 },
{ "metadata":1 },
{ "groupID":1 },
{ "departmentID":1 },
{ "assignedToID":1 },
{ "allowedUsers":1 },
{ "typeID":1 },
{ "isFileProhipted":1 },
{ "fileWriter":1 },
{ "assignedByID":1 },
{ "statusID":1,"metadata":1 },
{ "groupID":1,"metadata":1 },
{ "departmentID":1,"metadata":1 },
{ "assignedToID":1,"metadata":1 },
{ "allowedUsers":1,"metadata":1 },
{ "typeID":1,"metadata":1 },
{ "isFileProhipted":1,"metadata":1 },
{ "fileWriter":1,"metadata":1 },
{ "assignedByID":1,"metadata":1 },
{ "statusID":1,"groupID":1 },
{ "metadata":1,"groupID":1 },
{ "groupID":1,"groupID":1 },
{ "departmentID":1,"groupID":1 },
{ "assignedToID":1,"groupID":1 },
{ "allowedUsers":1,"groupID":1 },
{ "statusID":1,"departmentID":1 },
{ "metadata":1,"departmentID":1 },
{ "groupID":1,"departmentID":1 },
{ "departmentID":1,"departmentID":1 },
{ "assignedToID":1,"departmentID":1 },
{ "allowedUsers":1,"departmentID":1 },
{ "typeID":1,"departmentID":1 },
{ "isFileProhipted":1,"departmentID":1 },
{ "fileWriter":1,"departmentID":1 },
{ "assignedByID":1,"departmentID":1 },
{ "statusID":1,"assignedToID":1 },
{ "metadata":1,"assignedToID":1 },
{ "groupID":1,"assignedToID":1 },
{ "departmentID":1,"assignedToID":1 },
{ "assignedToID":1,"assignedToID":1 },
{ "allowedUsers":1,"assignedToID":1 },
{ "typeID":1,"assignedToID":1 },
{ "isFileProhipted":1,"assignedToID":1 },
{ "fileWriter":1,"assignedToID":1 },
{ "assignedByID":1,"assignedToID":1 }
])
Download the shardedCollStats.js
script and save it.
To verify the behavior of this method, from the mongo shell try the following:
// this should fail now
db.test1.stats({ indexDetails: true, scale: 1024 });
load('shardedCollStats.js');
// this should succeeed
shardedCollStats("test","test1","user","password");