I've been working on some simple scripts to run on mongo from the bash mand-line. Originally, I ran them as follows:
$ mongo dbname script.js
but I recently came across mikemaccana's answer, , indicating the use of mongo as an interpreter so I can just execute script.js (or any name I choose, with or without the .js) from the mand line.
$ script.js
I think it's brilliant and clean, but now I'd like to pass in a database name as a mand line argument.
$ script.js dbname
Here I use the bash-style "$1" to demonstrate what I'm doing in script.js.
#!/usr/bin/env mongo
var db = new Mongo().getDB($1);
// Do other things with db, once I resolve the name from the mand line.
This results in a "ReferenceError: $1 is not defined ...", which is not surprising. But how would I reference mand line arguments? Is this going to be a mongo convention? a javascript convention? Is it possible? It would make my mand-line experience with mongo much better aesthetically.
I've been working on some simple scripts to run on mongo from the bash mand-line. Originally, I ran them as follows:
$ mongo dbname script.js
but I recently came across mikemaccana's answer, https://stackoverflow./a/23909051/2846766, indicating the use of mongo as an interpreter so I can just execute script.js (or any name I choose, with or without the .js) from the mand line.
$ script.js
I think it's brilliant and clean, but now I'd like to pass in a database name as a mand line argument.
$ script.js dbname
Here I use the bash-style "$1" to demonstrate what I'm doing in script.js.
#!/usr/bin/env mongo
var db = new Mongo().getDB($1);
// Do other things with db, once I resolve the name from the mand line.
This results in a "ReferenceError: $1 is not defined ...", which is not surprising. But how would I reference mand line arguments? Is this going to be a mongo convention? a javascript convention? Is it possible? It would make my mand-line experience with mongo much better aesthetically.
Share Improve this question edited May 23, 2017 at 11:47 CommunityBot 11 silver badge asked Jun 22, 2014 at 14:30 mightypilemightypile 8,0523 gold badges40 silver badges44 bronze badges3 Answers
Reset to default 2Currently there is no way to do this using the mongo shell...
https://groups.google./forum/#!topic/mongodb-user/-pO7Cec6Sjc
... try using a bash script (or other scripting language you are fortable with) if you want to get a similar mand line experience.
Duplicate of How to pass argument to Mongo Script In a nutshell, this is not possible but several workarounds are given in the answers (not repeated here).
You can pass args to your script through
mongo --eval 'var databasePassword="password"' script.js
and you can access databasePassword value inside script.js
db.auth({ user: 'testUser, pwd: databasePassword });
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745392310a4625718.html
评论列表(0条)