且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将环境变量传递给mongo脚本

更新时间:2022-05-02 09:20:32

这对我有用:

mongo --eval "var my_var = '$MY_VAR'" my_script.js

忽略<. mongo将在命令行上处理所有剩余的参数作为要执行/解释的文件,但是显然将shell输入重定向与--eval结合使用会导致javascript名称空间被重置.

Leave out the <. mongo will process any remaining arguments on the command line as files to be executed/interpreted, but apparently combining the shell input redirect with --eval causes the javascript namespace to be reset.

我假设但不能确认这是因为作为参数传递的文件名是通过load()机制处理的,根据

I assume but cannot confirm that this is because filenames passed as arguments are processed via the load() mechanism, which according to https://docs.mongodb.com/v3.2/reference/method/load/, behaves as follows:

在使用load()执行文件之后,您可以引用mongo shell环境中定义该文件的任何函数或变量.

After executing a file with load(), you may reference any functions or variables defined the file from the mongo shell environment.