Since Mongo 4.0.5
it is possible to utilize some of the new admin functions to load text files and execute programs.
So you can write a Javascript function to execute a shell script that provides the value of an environment variable:
function getEnvValue(envVar, defVal) {
var ret= run("sh", "-c", `printenv ${envVar} >/tmp/${envVar}.txt`);
if (ret != 0) return defVal;
return cat(`/tmp/${envVar}.txt`)
}
And then using it like this:
db.createCollection("myCol",
{ capped: true,
size: getEnvValue('MYCOL_MAX_SIZE_GB', 2)*1024*1024*1024
});
This relies on the host shell but was acceptable in my case (Docker).
Was also trying to read the environ
directly:
cat('/proc/self/environ').split('\0') # Doesn't work
but it only gets the first variable. I guess because of the null characters...