MongoDB operations from batch script in windows

2,671 views
Skip to first unread message

vishal joshi

unread,
May 16, 2016, 7:00:30 AM5/16/16
to mongodb-user
Hello all,

        I have a very important question....please help....
                 i want to create a batch script which runs all create database, create collection and inserting, deleting data etc. commands, but i have not found any reference on this anywhere.................i really need it.....

i have written a script for starting the database automatically but want to run all queries...the starting mongoDB script is here....


##setting the path for java as well as mongoDB variables.
set path=C:\Program Files\Java\jdk1.8.0_71\bin;C:\Program Files\Java\jdk1.8.0_71;C:\Program Files\MongoDB\Server\3.2\bin
pause
start mongod --config C:\data\mongo.config --dbpath C:\data\db

pause
start mongo



can anybody please help me so that i can create database, create collections and run other important queries..............................from batch script......................in windows......    .bat file

help me here please......................if possible can you show me some sample script....

Thank you.

vishal joshi

unread,
May 17, 2016, 1:38:37 AM5/17/16
to mongodb-user
please somebody answer fast....

Kevin Adistambha

unread,
May 26, 2016, 12:25:48 AM5/26/16
to mongodb-user

Hi,

i want to create a batch script which runs all create database, create collection and inserting, deleting data etc. commands, but i have not found any reference on this anywhere……………..i really need it…..

i have written a script for starting the database automatically but want to run all queries…the starting mongoDB script is here…

One way to achieve this is to use scripting on the mongo shell. For example, to insert a document {x:1} into a collection coll in database test and print the content of the collection, you can create a script called e.g. script.js, which contains:

db = db.getSiblingDB('test')
db.coll.insert({x:1})
result = db.coll.find().toArray()
printjson(result)

and run the script from the command line or inside a batch script:

mongo script.js

Please note that any shell helper command such as use db or show collections does not work in a mongo shell script. This is explained in the Differences between Interactive and Scripted mongo page.

You may find these links helpful:

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages