Zlorg
Zlorg

Reputation: 58

MongoDB remote shell script create database

New to this forum, to mongodb , My goal is to automate a database creation from a linux BATCH on a computer client to the server.

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

Can't found the way in the Shell Helper , or perhaps i missed it in mongodb help: http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/

Is there is a solution or do i have to use Py -Pymongo or an another language?

Thank you, sorry for the first post i was not a really nice post.

Upvotes: 1

Views: 14811

Answers (1)

Stennie
Stennie

Reputation: 65363

If you want to create/use a database from a command line passed to the mongo shell you can either:

  • use the scripted equivalent of use NewDatabase in your JavaScript file:

    db = db.getSiblingDB('NewDatabase');

  • pass the database name your script should execute against on the mongo shell command line:

    mongo NewDatabase foo.js

If you don't specify a database name, test is the default name used by the mongo shell.

Upvotes: 8

Related Questions