如何将参数传递给Mongo Script [英] How to pass argument to Mongo Script
本文介绍了如何将参数传递给Mongo Script的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用这样的mongo和脚本文件:
I've been using mongo and script files like this:
$ mongo getSimilar.js
我想将参数传递给文件:
I would like to pass an argument to the file:
$ mongo getSimilar.js apples
然后在脚本文件中选择传入的论点。
And then in the script file pick up the argument passed in.
var arg = $1;
print(arg);
推荐答案
使用 - eval
并使用shell脚本来修改传入的命令。
Use --eval
and use shell scripting to modify the command passed in.
mongo --evalprint('apples');
或者制作全局变量(归功于Tad Marshall):
Or make global variables (credit to Tad Marshall):
$ cat addthem.js
printjson( param1 + param2 );
$ ./mongo --nodb --quiet --eval "var param1=7, param2=8" addthem.js
15
这篇关于如何将参数传递给Mongo Script的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文