user13473927
user13473927

Reputation:

Mongo commands using Batch Script

I am trying to execute a batch script to create a collection and insert data into it. It is opening Mongo.exe but it is not executing the commands.

I have tried the following ways.

cd  C:\Program Files\MongoDB\Server\3.2\bin
mongo.exe
mongo.exe --eval "use MyDatabase"
mongo.exe --eval  "db.TestCollection.insert({_id: 'T1' , seq : 1})"
mongo.exe --eval  "db.TestCollection.insert({_id: 'T2' , seq : 2})"
pause

The above script opens Mongo.exe and does nothing other than that. I need help as I want to run every command in one connection with mongodb. So I tried:

mongo.exe mydb D:\Delta\scriptfile.js

my scriptfile.js contains:

print(db)
db.TestCollection.insert({_id: 'T1' , seq : 1})
db.TestCollection.insert({_id: 'T2' , seq : 2})
print(show collections)
print(db)

But after this it is showing that path of file D:\Delta\a.js not found.

Upvotes: 2

Views: 2157

Answers (1)

Rishav Singh
Rishav Singh

Reputation: 343

Modify your code and try:

cd  /D C:\Program Files\MongoDB\Server\3.2\bin
mongo.exe mydb < D:\Delta\scriptfile.js

with your scriptfile.js as same:

print(db)
db.TestCollection.insert({_id: 'T1' , seq : 1})
db.TestCollection.insert({_id: 'T2' , seq : 2})
print(show collections)
print(db)

Upvotes: 3

Related Questions