2

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.

1
  • 1
    Not a solution, but cd should actually read cd /D in order to also change the drive if necessary... Commented May 5, 2020 at 13:49

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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