Mongosh command not working in Bash shell

First of all, a bit of background: I’m currently working through a Udemy course. The instructor demos installing MongoDB 4.0, then running mongod in hyper (as the instructor is using a Mac), then opening a new shell and running mongo.

I’m trying to achieve the same outcome with MongoDB 6.0.3. I know that now MongoDB uses a separate shell, mongosh. I downloaded the shell and put it in the bin with the other MongoDB downloads. I also set the path environmental variables. When I run the mongod command in the Bash terminal, it seems to be working, but when I open a new Bash shell and run mongosh, it’s giving me errors. I tried the commands for mongo or mongos, and still not working.

I wanted to share screencaps here, as I think that’d be more helpful, but it looks like I’m going to need to clean off some diskspace to run Photoshop, so for now I hope these descriptions will help. Here is what I’m seeing in the Bash shell when I run each command:

“$ mongosh
bash: mongosh: command not found”

“$ mongo
BadValue: error: no args for --configdb
try ‘C:\Program Files\MongoDB\Server\6.0\bin\mongos.exe --help’ for more information”

I double-checked, and my mongosh file is definitely in “C:\Program Files\MongoDB\Server\6.0\bin”, which is the path I set in the environmental variables section.

I followed what this guy did in the video: Mongo command is not working/found in MongoDB 6.0+ || MongoDB error fix - YouTube – with the one difference being that I put my mongosh file with the other MongoDB files in the bin folder… though I don’t see that this would make a difference. But when I run the “mongosh” command in Bash, I get the error above. I’m confused, as it seems to work fine for him in the tutorial video, so I came here, as I’m quite frustrated with trying to troubleshoot at this point. I’ve been troubleshooting MongoDB installations issues for a while now, between this and other differences in the updated version, and browsing here and Stack Overflow, but still confused as to why it’s not working.

Any help would be appreciated! Thanks!

Edit: Oh my gosh. I just decided to try the command CLI rather than the Bash shell, and… it works on the command prompt, but not the Bash shell. I suppose I can use the command prompt, but now I’m confused why it’s not working in the Bash shell. I feel a little silly realizing this after typing all that, but I do prefer to work in the Bash shell if possible.


Hi @Amber_Adamson! I don’t have a complete answer for you, but here’s the troubleshooting that I’d do if I were in your situation:

  • echo $PATH – Just double-checking that the path environment variable is set to what it is supposed to be set
  • which mongosh vs. type mongosh – Does the behavior for finding mongosh differ between bash itself and its subprocesses? (Maybe also compare with which mongo / which mongod just to be sure that those pick up the right directory)
  • hash -r – Clear bash’s cache for executable paths, then run mongosh again
  • And finally, can you run mongosh by specifying its full path inside bash, or is it only typing the literal single word mongosh that fails?

Finally, this is not related to troubleshooting but points to something being wrong in general:

$ mongo
BadValue: error: no args for --configdb
try ‘C:\Program Files\MongoDB\Server\6.0\bin\mongos.exe --help’ for more information

mongo and mongos are different executables, so if mongo self-identifies as mongos and reports an error that only mongos should ever really report, there’s most likely something else wrong with the MongoDB installation here as well.

1 Like

How are you running bash on Windows? Is this in WSL?

You would need to install mongosh inside WSL if you are.

1 Like

I found this post looking for a way to make the mongoDB shell work properly in bash, which I installed on Windows 10 as part of a git installation. Everything works, but whilst mongosh works perfectly using the Windows CLI, all the nice features of mongosh are lost in bash. Indeed, I found it quite error prone in bash, so I just keep on using it with the CLI.

What I did on windows was, I installed mongosh and created an alias and placed it’s location in .bash_profile using vim, just below mongod and mongos

1 Like

Thanks, Krimier_Dan_Sanz. Your response prompted me to put some more effort to the problem, and I now found the solution that works best for me. I can simply run the mongosh shell in a Visual Studio Code terminal, and it works perfectly.

Just putting the solution here for anyone looking:

Install MongoDB

To install MongoDB (version 5.0) on WSL (Ubuntu 20.04):

  1. Open your WSL terminal (ie. Ubuntu) and go to your home directory: cd ~
  2. Update your Ubuntu packages: sudo apt update
  3. Import the public key used by the MongoDB package management system: wget -qO - | sudo apt-key add -
  4. Create a list file for MongoDB: echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
  5. Reload local package database: sudo apt-get update
  6. Install MongoDB packages: sudo apt-get install -y mongodb-org
  7. Confirm installation and get the version number: mongod --version
  8. Make a directory to store data: mkdir -p ~/data/db
  9. Run a Mongo instance: sudo mongod --dbpath ~/data/db
  10. Check to see that your MongoDB instance is running with: ps -e | grep 'mongod'
  11. To exit the MongoDB Shell, use the shortcut keys: Ctrl + C


I’ve been asked to post my solution for running the mongosh shell in VS Code. I’ll gladly do so, with the understanding that this is not specifically a solution to the OP’s question about “mongosh command not working in Bash shell”. I understand that the OP’s question has been resolved. Running mongosh in a Bash terminal was suboptimal to me, and running it in a VS Code terminal became my preferred solution; not least, because I’m also using this for learning web development. Assuming a problem-free installation of VS Code on Windows 10, my solution was really quite simple. Just add the mongosh.exe path to the sytem’s Path environment variable; in my case C:\Program Files\MongoDB\Shell\bin. Open a terminal in VS Code, which in my case by default runs Powershell. At the PS prompt, enter: mongosh. The terminal now runs the mongosh shell, and displays the usual mongosh prompt. Two advantages:

  1. I prefer to type closed brackets first, like:
    test> db.products.insertOne({})
    and then complete the command, like:
    test> db.products.insertOne({_id: 1, name: “Cool product”, price: 0.99})
    This requires moving the cursor back into the previously typed line. Although it also seems to work, at first sight, when running mongosh in Bash, executing the command returns an error.
  2. In a VS Code terminal running mongosh, I can use the up/down arrows to cycle through previous commands, and edit them as needed to quickly create a new command. This doesn’t work when running mongosh in Bash.

I’m the OP, coming back to this a few months later. First of all, I have to say, I’m really embarrassed to admit I used some imprecise terminology in my original post… which undoubtedly led to confusion. I was working with the Gitbash terminal on my Windows, which I was referring to as “Bash” as short for GITBASH… which was undoubtedly confusing, since anyone would assume I was using a Linux Bash shell, maybe via an Ubuntu VM or something. I have since worked with a Linux Bash shell, so I would be a lot more precise with my terminology and differentiation now. Though I know Git Bash reproduces Bash features, I still think I should’ve been a bit more precise about what I was working with, as I basically led everyone to assume I was working with a Linux shell.

I’m not sure I ever figured out why mongosh wasn’t working on Gitbash. I just started using the command prompt instead, as it worked.

1 Like

Hello! I think I solved this problem. After downloading “mongoDB shell”, make sure that it is saved in “Program Files” on your C drive. You should be able to access this from “My PC” then you have to go to that file “mongosh” and go to bin, then make a copy of the file that has a little green leaf on it. Take the copy and go back into “Program Files” where you originally downloaded your monggDB-different from mongosh. Open that folder until you get to bin. Add a coy of mongosh to the bin of your mongoDB. Also, Make sure your mongoDB bin is in “system enviorenmental variables” I hope this helps someone!

I think I have it figured out.

  1. Make sure you have your mongosh folder within program files. Previously the OP said they placed the shell in the bin with the other MongoDB downloads. This should be fixed and you can do it by simply dragging the shell into program files.
  2. Make sure you copy and paste the shell’s new address and set it up in the environmental variables.
  3. If you are following the udemy course, you would have had two aliases already in vim .bash_profile. Add another alias for mongosh in the same manner as the previous two aliases. ie. write alias mongod=“/c/Program\ files/MongoDB/Server/7.0/bin/mongod.exe”. (This is for version 7.0 of MongoDB).
  4. Now you should be able to run it with gitbash by writing mongod and then mongosh.

Just writing this here because I had similar problems with the mongosh shell in a vs code terminal. This was the solution I mustered up after thinking about how to solve the problem for a while.