Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call MongoDB commands using .bat file

I'm trying to execute MongoDB command (for create DB) using .bat file.

For that I've tried:

cd C:\Program Files\MongoDB\Server\3.4\bin
mongo 
mongo --eval "use MyDatabase"
pause

But it gives an error missing ; before statement @(shell eval):1:4
How can I solve this issue?

Side: I've already gone through MongoDB SyntaxError: missing ; before statement @(shell)

like image 222
Divyang Desai Avatar asked Dec 30 '16 08:12

Divyang Desai


1 Answers

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

like image 159
Sparw Avatar answered Sep 24 '22 07:09

Sparw