如何使用.bat文件调用MongoDB命令 [英] How to call MongoDB commands using .bat file

查看:602
本文介绍了如何使用.bat文件调用MongoDB命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.bat文件执行MongoDB命令(用于创建数据库).

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

但是它给出了错误missing ; before statement @(shell eval):1:4
我该如何解决这个问题?

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

方面:我已经经历过 MongoDB SyntaxError:缺少;语句之前@(shell)

推荐答案

可以尝试:

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

我正在使用.bat文件,该文件可以正常运行并包含此文件

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

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

编辑

我测试了这样的文件,并且可以正常工作(创建数据库,集合和文档)

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

编辑2

如果您想在后台运行.bat文件,我已经制作了一个可以正常工作的.VBS文件

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

希望有帮助

这篇关于如何使用.bat文件调用MongoDB命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆