Unix type operators in MongoDB shell

Hi,
Is there a way to save the history of command run to a file or redirect to a file (like in unix “fc” or “> filename” )

1 Like

The mongo shell seems to keep an history by default, at least on my Arch Linux. Using the cursor up brings previous commands and let you edit them.

that is true. arrows up and down put previous commands under the cursor. But I want to save the whole history to a file just in case I want to review the labs or exercises. Copy-paste is a solution , should it be a better way.

1 Like

If I may add. When I do things like trying to get the pipeline working and I know I will have to retype often more or less the same thing I always do it in a script and use stdin redirection to execute it. In my script I put trace that help me find my errors. For example for one of the lab I write

var countries_USA = { "countries" .... }

countries_USA
var sort = { ‘$sort’ : … }
sort
var pipeline = {
{ '$match : [ … , countries_USA , … ] } ,
sort
}

The shell will output all my intermediate variables and if a syntax error occurs, and it does often 8-(, I can pinpoint the error faster.

1 Like

I usually open up a text editor and input all my ‘statements’ there before copy paste to mongo shell.

1 Like