Javascript 如何将参数传递给 Mongo 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10114355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass argument to Mongo Script
提问by Brig
I've been using mongo and script files like this:
我一直在使用这样的 mongo 和脚本文件:
$ 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 = ;
print(arg);
回答by jcollum
Use --eval
and use shell scripting to modify the command passed in.
使用--eval
和使用shell脚本来修改传入的命令。
mongo --eval "print('apples');"
mongo --eval "print('apples');"
Or make global variables (credit to Tad Marshall):
或者创建全局变量(归功于 Tad Marshall):
$ cat addthem.js
printjson( param1 + param2 );
$ ./mongo --nodb --quiet --eval "var param1=7, param2=8" addthem.js
15
回答by kristina
You can't do that, but you could put them in another script and load that first:
你不能这样做,但你可以把它们放在另一个脚本中并先加载它:
// vars.js
msg = "apples";
and getSimilar.js was:
getSimilar.js 是:
print(msg);
Then:
然后:
$ mongo vars.js getSimilar.js
MongoDB shell version: blah
connecting to: test
loading file: vars.js
loading file: getSimilar.js
apples
Not quite as convenient, though.
不过没那么方便。
回答by robodo
Set a shell var:
设置一个 shell 变量:
password='bladiebla'
Create js script:
创建js脚本:
cat <<EOT > mongo-create-user.js
print('drop user admin');
db.dropUser('admin');
db.createUser({
user: 'admin',
pwd: '${password}',
roles: [ 'readWrite']
});
EOT
Pass script to mongo:
将脚本传递给 mongo:
mongo mongo-create-user.js
回答by Tony
I used a shell script to pipe a mongo command to mongo. In the mongo command I used an arg I passed to the shell script (i.e. i used $1
):
我使用 shell 脚本将 mongo 命令通过管道传递给 mongo。在 mongo 命令中,我使用了一个传递给 shell 脚本的参数(即我使用了$1
):
#!/bin/sh
objId=
EVAL="db.account.find({\"_id\" : \"$objId\"})"
echo $EVAL | mongo localhost:27718/balance_mgmt --quiet
回答by user764486
I wrote a small utility to solve the problem for myself. With the mongoexec
utility, you would be able to run the command ./getSimilar.js apples
by adding the following to the beginning of your script:
我自己写了一个小工具来解决这个问题。使用该mongoexec
实用程序,您可以./getSimilar.js apples
通过将以下内容添加到脚本的开头来运行该命令:
#!/usr/bin/mongoexec --quiet
Within the script, you can then access the arguments as args[0]
.
在脚本中,您可以将参数作为args[0]
.
回答by beac0n
I solved this problem, by using the javascript bundler parcel: https://parceljs.org/
我通过使用 javascript bundler 包裹解决了这个问题:https://parceljs.org/
With this, one can use node environment variables in a script like:
有了这个,可以在脚本中使用节点环境变量,例如:
var collection = process.env.COLLECTION;
when building with parcel, the env var gets inlined:
使用包裹构建时,环境变量被内联:
parcel build ./src/index.js --no-source-maps
The only downside is, that you have to rebuild the script every time you want to change the env vars. But since parcel is really fast, this is not really a problem imho.
唯一的缺点是,每次要更改环境变量时都必须重新构建脚本。但是由于包裹真的很快,所以恕我直言,这并不是真正的问题。