MongoDB远程Shell脚本创建数据库

Zlo*_*org 0 mongodb

这个论坛的新手,对mongodb来说,我的目标是自动化从计算机客户端上的Linux BATCH到服务器的数据库创建。

conn = new mongo();    "returns OK"
db = conn.getDB("admin"); "returns OK"
db.runCommand( { use NewDatabase } ) "returns *NOK* , this  is not the good syntax  "
Run Code Online (Sandbox Code Playgroud)

在Shell Helper中找不到方法,或者我在mongodb帮助中错过了它:http : //docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/

有解决方案还是我必须使用Py -Pymongo或其他语言?

谢谢,对不起我的第一篇文章我不是一个很好的文章。

Ste*_*nie 5

如果要通过传递给mongoshell 的命令行创建/使用数据库,则可以:

  • 使用JavaScript文件中的脚本等效use NewDatabase

    db = db.getSiblingDB('NewDatabase');

  • 传递您的脚本应在mongoshell命令行上执行的数据库名称:

    mongo NewDatabase foo.js

如果您未指定数据库名称,test则是mongoShell 使用的默认名称。