1

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.

6
  • 1
    Please format your code. Commented Jan 19, 2015 at 23:11
  • 1
    Can you be more specific on what is not working? Do you have any errors? If you are trying to include use dbname in your command-line javascript, that won't work (it's interactive syntax only). For tips see: Write Scripts for the mongo Shell and in particular note the differences between interactive and scripted mode. You might also be interested in the MongoDB manual examples discussing how to Model Tree Structures. Commented Jan 20, 2015 at 0:17
  • Sorry, i willl be more explicit. In fact : How to create a minmal javascript file Commented Jan 20, 2015 at 9:14
  • @nwk Thank's a lot for the link . Everything'work great , except create a new db, can't find the command to do that: db._adminCommand( {use a-new-db} )' not working but work in interactive shell. Commented Jan 20, 2015 at 10:42
  • @nwk Thank's a lot for the link . Everything'work great , except create a new db, can't find the command to do that: inline 'db._adminCommand( {use a-new-db} )', not working but work in interactive shell. Commented Jan 20, 2015 at 10:48

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Work fine after test, have to insert one document to commit DB creation.Thank's a lot , i have not seen i this behaviour in mongo doc last days.
@Zlorg Yes, the database files will not be created until you insert a document or create an index for a collection in that database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.