VSCode中无法调用mongosh连接MongoDB Shell问题求助
VSCode中MongoDB扩展连接mongosh报错的解决方法
问题描述
在VSCode中使用官方MongoDB for VS Code扩展连接MongoDB Shell时,直接进入mongosh的bin目录可正常启动,但在VSCode中执行命令时始终报错,提示mongosh不被识别为可执行命令。已将环境变量设置为C:\Program Files\mongosh-1.8.2-win32-x64\bin,具体报错信息如下:
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
解决步骤
- 重启VSCode及终端:环境变量修改后,已打开的VSCode和终端不会自动加载新的环境变量,关闭所有VSCode窗口后重新打开,再尝试执行命令。
- 验证环境变量有效性:在VSCode的终端中执行对应命令检查路径:
- PowerShell:
echo $Env:Path - CMD:
echo %PATH%
查看输出中是否包含C:\Program Files\mongosh-1.8.2-win32-x64\bin,如果没有,确认环境变量是添加到系统环境变量而非仅用户环境变量,添加完成后重启电脑再测试。
- PowerShell:
- 指定mongosh完整路径:若环境变量仍不生效,直接在VSCode扩展设置中指定mongosh的绝对路径:
- 打开VSCode设置(快捷键
Ctrl+,) - 搜索
MongoDB: Shell Path - 将值设置为
C:\Program Files\mongosh-1.8.2-win32-x64\bin\mongosh.exe
- 打开VSCode设置(快捷键
- 测试终端直接执行:在VSCode终端中手动输入完整路径
C:\Program Files\mongosh-1.8.2-win32-x64\bin\mongosh.exe,如果能正常启动,说明问题出在环境变量未被VSCode识别,按前两步操作即可。
内容的提问来源于stack exchange,提问作者Shresth Gour
相关产品推荐
相关产品推荐

