0

I cannot connect to mongodb shell using VScode . It is running fine if I open it by going to the extracted location of the bin.
This error pops up everytime I try to open mongoDB shell and I have set the environment variable as the location of the mongodb shell Environmental Variable location: 'C:\Program Files\mongosh-1.8.2-win32-x64\bin'
I am using the official MongoDB for VS Code extension in VScode

mongosh $Env:MDB_CONNECTION_STRING;
mongosh : The term 'mongosh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or 
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ mongosh $Env:MDB_CONNECTION_STRING;
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (mongosh:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
  • Did you add the location of `mongosh.exe` to your `PATH` environment variable? – Wernfried Domscheit May 14 '23 at 17:50
  • yes i did add the mongosh.exe – Shresth Gour May 14 '23 at 17:52
  • I added this location 'C:\Program Files\mongosh-1.8.2-win32-x64\bin' – Shresth Gour May 14 '23 at 17:53
  • 1) did you restart the shell after editing the system `PATH`? You need to. processes do not automatically pick up updates to the system environment. 2) Please always [edit] clarifications into your question post instead of hiding them in the comments! Comments are for _soliciting_ clarifications- not for providing them. – starball May 14 '23 at 18:55
  • Yes I did restart my device – Shresth Gour May 15 '23 at 03:40
  • Does this answer your question? ["Register" an .exe so you can run it from any command line in Windows](https://stackoverflow.com/questions/4822400/register-an-exe-so-you-can-run-it-from-any-command-line-in-windows) – starball Aug 27 '23 at 23:41

2 Answers2

0

I just solved this same problem!

The way I made it is that you have to add the filepath to the user environment variables, not system variables, more specifically I added it to the "Path" variable in the User variables.

So the steps that I used would be:

  1. get the filepath to the "mongosh.exe" file (The file path without the "\mongosh.exe" at the end, so it should look something like this: "...\mongosh-1.10.6-win32-x64\bin").
  2. Open environment variables
  3. In the top part of the environment variables tab, the will be a seccion called "User variables for your user", under this there will be a box containing a number of variables, in those there will be one called "Path", this is the one we are going to edit, so click on it, it should become blue.
  4. To edit this variable, still in the top part, click on the button that says "Edit..." (notice that this is the button that is below all the variables in the top section, I specify this since there is two sections that contain the same buttons, so just stick to the top one).
  5. Once you clicked edit, it should have opened another tab called "Edit environment variable", this tap is a list of file paths (or with none in case you have never enter a file path there), with some buttons on the left. You need to click the button that says "New".
  6. Once you clicked "New" it should have added a place where you can put the filepath to the "mongosh.exe" file, put the file path, and hit enter.
  7. Then press "ok" at the bottom left, to confirm the changes.
  8. In the "Environment Variables" tab, press "ok" in the bottom left to finalize the process.

Now it is done. For good measured I rebooted my pc when I did this, but I'm not sure if it is necessary, perhaps it suffices with just restarting VScode, but like I said, I rebooted my pc for good measure.

This is wat worked in my case, because I was following in a YouTube video that didn't work for me. I don't know why it is the case, but I had this Idea since I once had to install a MinGW (c++ compiler) and this is what had to be done.

Anyways, hope it helped ! :)

-1

I had the same issue, I fixed it like so : 1 -Ensure you've added the \ at the end of filepath in system environment variables. 2 - reload vscode. you can refer to this link on how to install mongosh : https://www.mongodb.com/docs/mongodb-shell/install/#std-label-mdb-shell-install

  • I'm pretty sure a trailing slash is not required, and the user already said they restarted their VS Code (implicitly by restarting their device) after the environment modifications. Read the comments. – starball May 19 '23 at 03:20