Is there a way to read a line from the Mongo shell? readline()
is not defined and neither is system.stdin
.
I need to do this in interactive mode, as opposed to feeding input to a script executed by the MongoDB shell.
Is there a way to read a line from the Mongo shell? readline()
is not defined and neither is system.stdin
.
I need to do this in interactive mode, as opposed to feeding input to a script executed by the MongoDB shell.
Share Improve this question asked Jul 4, 2012 at 17:19 SimSim 13.5k11 gold badges69 silver badges96 bronze badges 4- What are you trying to acplish with this? – Sergio Tulentsev Commented Jul 4, 2012 at 17:20
- @Sergio Introduce human input in the middle of a sequence of admin operations based on output printed to the screen. It's much more convenient to just input the required info and continue processing as opposed to break the operation up into multiple JS functions. – Sim Commented Jul 4, 2012 at 17:22
- 1 Hm, interesting. I never thought of this use case. Probably 10gen did not as well :) – Sergio Tulentsev Commented Jul 4, 2012 at 17:30
- 2 The Mongo shell (2.0.6) currently isn't providing a fully programmable interpreter like you are envisioning .. the driver implementations would be best suited for that. It would be useful to add your suggestions to the Jira queue for consideration. – Stennie Commented Jul 5, 2012 at 1:03
2 Answers
Reset to default 7Per @Stennie's ment, this is not possible right now.
Officially, this is not possible in Mongo shell.
Unofficially, yes it is possible. There is one small hack you can use to read user input.
Mongo shell among many undocumented functions, contains one function named passwordPrompt
which can be used to read user input.
Just, there are some limitations with this hack you must be aware of.
- This function, once called, prints string
Enter password:
to console, and has no option to change this prompt. Since this function is native (non js) it is not possible to redefine it to remove prompt. - This function will not show you what you type as you type it (makes sense since mongo shell uses it internally for entering user passwords). You will have to type in your input blindly.
But if you do not mind having "Enter password:" prompt appearing each time you wish to get user input, and not seeing what you type, then this should work.
Here is one example. You can run it in both interactive mode or write it inside .js file:
user_input = passwordPrompt();
print("user inputed: " + user_input);
If you run it and type "hack nation", output from this will be:
Enter password:
user inputed: hack nation
Also, Mongo shell allows you to run OS mands from shell itself, what allows you to handle user input using non-mongo and non-javascript utilities. For example, I have created programs for mongo shell which use windows powershell and framework to make graphic user interface and interact with user using gui, and return user input back to Mongo shell. I prefer this over passwordPrompt
.
There are quite a few undocumented functions in Mongo shell you can use to do some more advanced things, such as getting user input, file system I/O functions, etc.