Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

javascript - How to pass argument to Mongo Script

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use --eval and use shell scripting to modify the command passed in.

mongo --eval "print('apples');"

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...