如何将环境变量传递给mongo脚本 [英] How to pass environment variable to mongo script

查看:58
本文介绍了如何将环境变量传递给mongo脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将名为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()机制处理的,根据

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.

这篇关于如何将环境变量传递给mongo脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆