How to invoke MongoDB commands using a .bat file

I am trying to execute the MongoDB command (to create a database) using a .bat file.

For this I tried:

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

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

Side: I already passed MongoDB SyntaxError: missing; before the @ operator (shell)

+6
source share
2 answers

You can try:

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

I am using a .bat file that works correctly and contains this

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

EDIT

I tested such a file 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 in the background, I created a .VBS file that works correctly

mongodb.VBS

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

Hope this helps

+7
source
  • Put this code in the bat file.
  • It will launch and activate mongodb in two different CMD windows with administrator privileges.
  • Before setting up your environment, check the paths.

START "runas / user: administrator" cmd / K "cd C: \ Program Files \ MongoDB \ Server \ 3.4 \ bin and 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 and mongo.exe"

+1
source

Source: https://habr.com/ru/post/1013629/


All Articles