10

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.

Sim
  • 13,147
  • 9
  • 66
  • 95
  • What are you trying to accomplish with this? – Sergio Tulentsev Jul 04 '12 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 Jul 04 '12 at 17:22
  • 1
    Hm, interesting. I never thought of this use case. Probably 10gen did not as well :) – Sergio Tulentsev Jul 04 '12 at 17:30
  • 2
    The Mongo shell (2.0.6) currently isn't providing a fully programmable interpreter like you are envisioning .. the [driver](http://www.mongodb.org/display/DOCS/Drivers) implementations would be best suited for that. It would be useful to [add your suggestions](https://jira.mongodb.org/browse/SERVER/component/10003) to the Jira queue for consideration. – Stennie Jul 05 '12 at 01:03

2 Answers2

7

Per @Stennie's comment, this is not possible right now.

Sim
  • 13,147
  • 9
  • 66
  • 95
5

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.

  1. 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.
  2. 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 commands 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 .net 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.

SYOB SYOT
  • 900
  • 10
  • 14
  • This works with latest version of Mongo shell for sure, and had been tested. I do not know weather it works in all versions, and if it does not, in which it does not. – SYOB SYOT Jun 25 '19 at 19:23
  • It's documented here https://www.mongodb.com/docs/manual/reference/method/passwordPrompt/ – M. Adam Aug 17 '22 at 04:55