问题描述
我想将名为file_path
的环境变量传递给js文件.
I want to pass the environment variable named file_path
to js file.
我正在使用以下语法:
mongo --eval "var file_path='$file_path'" < aggregation.js
输出:
MongoDB shell version: 3.2.9-rc0
connecting to: test
注意:
它不执行aggregation.js
文件.只有通过参数.
It does not execute aggregation.js
file.only it pass the argument.
推荐答案
这对我有用:
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()
机制处理的,根据 https://docs.mongodb.com/v3.2/reference/method/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:
这篇关于如何将环境变量传递给mongo脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!