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

Hi Yuri_52502! It’s been a minute, but just in case anyone else has this question, I believe that MongoDB takes care of this for you. Check out documentation on shell logs here: https://www.mongodb.com/docs/mongodb-shell/logs/

1 Like

Hi @Yuri_52502,
You can utilize MongoDB for VS Code’s playgrounds to run a set of commands together and:

  • Edit and run your playgrounds to see the results immediately
  • MongoDB Syntax Highlighting
  • Intelligent autocomplete for commands, operators and database, collection and field names
  • Stage snippets to create aggregations quickly

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

1 Like