How to call MongoDB commands using .bat file

Can you try with :

cd Program Files\MongoDB\Server\3.4\bin
mongo.exe 
mongo.exe --eval "use MyDatabase"
pause

I am using a .bat file which works properly and contains this

cd \Program Files\MongoDB\Server\3.2\bin
mongod.exe
pause

EDIT

I tested a file like this and it works fine (create db, collection and document)

mongodb.bat

cd \Program Files\MongoDB\Server\3.2\bin
mongo.exe db-mydb --eval "db.yourCollection.insert({key:'value'});"
pause

EDIT 2

If you want to run your .bat file on the background, i have made a .VBS file which works properly

mongodb.VBS

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "C:\Path\To\Your\mongodb.bat" & Chr(34), 0
Set WshShell = Nothing

Hope it helps


  • Put this code in your bat file.
  • It'll start and activate the mongodb in 2 different admin privileged cmd windows.
  • Please check the paths before to adjust to your environment.

START "runas /user:administrator" cmd /K "cd C:\Program Files\MongoDB\Server\3.4\bin & mongod.exe --dbpath c:\data\db"

TIMEOUT /T 10 START

START "runas /user:administrator" cmd /K "cd C:\Program Files\MongoDB\Server\3.4\bin & mongo.exe "