如何将参数传递给 Mongo 脚本 [英] How to pass argument to Mongo Script
本文介绍了如何将参数传递给 Mongo 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用这样的 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 --eval "print('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 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文